#!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH

while [ $(ping -c 1 130.238.96.1 2>/dev/null | grep "bytes from" | awk 'END{print NR}') = "0" ]; do
 sleep 1
done

IFACE=$(for i in $(ifconfig | awk '/^[a-zA-Z]+[0-9]+/ {gsub(/\:/, ""); print $1}'); do if ifconfig $i | grep -q $(netstat -rn | awk '/default/ {gsub(/\./, " "); print $2"."$3"."$4}'); then echo $i; fi; done)
IPADDR=$(ifconfig ${IFACE} | awk '($1 == "inet") {sub(/\:/, ""); print $2}')
NETMASK=$(netstat -rn | grep $(echo ${IPADDR} | awk -F '.' '{print $1"."$2"."$3".0"}') | awk '{sub(/\//, " "); print $2}')
FULL_COMPUTERNAME=$(host $(echo ${IPADDR}) | awk '{print $5}' | awk -F '.' '{print $1}')
DOMAIN_NAME=$(host $(echo ${IPADDR}) | awk '{print $5}' | awk '{sub(/\.$/,"");printf("%s\n",substr($1,index($1,".")+1))}')
HOST_NAME=${FULL_COMPUTERNAME}.${DOMAIN_NAME}
if [ $(echo ${FULL_COMPUTERNAME} | awk -F "-" '{print $1"-"$2"-"$3"-"$4}') = "ovirt-0-1a-4a" ]; then
  SHORT_COMPUTERNAME=$(echo ${FULL_COMPUTERNAME} | awk -F "-" '{print $1"-"$5"-"$6"-"$7}')
  NETBIOS_COMPUTERNAME=$(echo ${SHORT_COMPUTERNAME} | awk '{print toupper ($1)}')
else
  NETBIOS_COMPUTERNAME=$(echo ${FULL_COMPUTERNAME} | awk '{print toupper ($1)}')
fi

SMBCONF="/usr/local/etc/smb.conf"
SUDOERS="/usr/local/etc/sudoers"
GROUP="/etc/group"
mount_smbfs -N -I storm.slu.se //vps_manager@storm/owners$ /mnt/
SUDOER=$(grep "${HOST_NAME}" /mnt/owners.txt | awk '{print $1}')
umount /mnt
rm /etc/nsmb.conf
mv /etc/nsmb.conf.bak /etc/nsmb.conf

hostname ${HOST_NAME}
sed -i '' "s/hostname=\"ovirt.vps.slu.se\"/hostname=\"${HOST_NAME}\"/" /etc/rc.conf
service syslogd restart

export LOGNAME=root

sed -i '' -e "s/netbios name = ovirt/netbios name = ${NETBIOS_COMPUTERNAME}/" -e "s/interfaces = 0.0.0.0\/00/interfaces = ${IPADDR}\/${NETMASK}/" ${SMBCONF}
net ads join -n ${NETBIOS_COMPUTERNAME} -U vps_manager%Yujk..45a createcomputer="Core/ComputerAccounts/VPS"
sed -i '' -e 's/#samba/samba/' -e 's/#winbindd/winbindd/' /etc/rc.conf
service samba start

if [ ! -z ${SUDOER} ]; then
  while [ $(net sam createbuiltingroup Administrators 2>&1 | grep -c 'winbind seems not to run') -gt 0 ]; do
    sleep 1
  done

  net sam createbuiltingroup Administrators
  net sam addmem Administrators "AD\Domain Admins"
  net sam createbuiltingroup Users
  net sam addmem Users "AD\Domain Users"

  while [ -z $(net groupmap list | awk '/Administrators/ {gsub(/\(/, ""); gsub(/\)/, ""); print $2}') ]; do
    sleep 1
  done

  while [ $(wbinfo -n ${SUDOER} 2>&1 | grep -c 'Could not lookup name') -gt 0 ]; do
    sleep 1
  done

  LOCAL_ADMIN_SID=$(net groupmap list | awk '/Administrators/ {gsub(/\(/, ""); gsub(/\)/, ""); print $2}')
  DOMAIN_LOCAL_ADMIN_SID=$(wbinfo -n ${SUDOER} | awk '{print $1}')
  net groupmap addmem ${LOCAL_ADMIN_SID} ${DOMAIN_LOCAL_ADMIN_SID}

  sed -i '' "/root ALL=(ALL) ALL/{G;s/$/${SUDOER} ALL=(ALL) ALL/;}" ${SUDOERS}
  sed -i '' "/wheel:\*:0:/ s/$/,${SUDOER}/" ${GROUP}
fi

sed -i '' -e 's/#puppet/puppet/' /etc/rc.conf
service puppet start
