#!/bin/bash
##########################################################
# this is obviously a post install for a beowulf-serial
# that will become more obvious shortly.
#########################################################
type="beowulf"
ip=`/sbin/ifconfig eth0 |grep 'inet addr' |awk {'print $2;'}|cut -c 6-`
hostname=`/usr/bin/host $ip|cut -d ' ' -f5 | cut -d. -f1` 
hostname $hostname
rhver=`rpm -q --qf %{version} redhat-release`
treepath="http://lanserver.mydomain.edu/rh-$rhver/tree/$type"


###############################################################
# Subroutines
###############################################################

# check for remote existence
# if so - remove the current one then put the new one in place
fix_file () {
  fn=`basename $1`
  dn=`dirname $1`
  cd $dn
  if [ -f $fn ]; then
     mv $fn ${fn}.pstinstall
  fi
  if wget -r -q -nd -nH $treepath/$1; then
	echo "$treepath/$1 -> $1"
        rm -f ${fn}.pstinstall
  else
        echo "$1 is missing or a dir on remote" 
        if [ -f ${fn}.pstinstall ]; then
          mv ${fn}.pstinstall $fn
        fi
  fi
  if [ -n "$2" ]; then
    chmod $2 $1
  fi
}


#######################################################
# misc stuff to fix
#######################################################

#turn tools and daemons on/off
chkconfig amanda on
chkconfig atd off
chkconfig nfs on
chkconfig nfslock on
chkconfig portmap on
chkconfig yum on
chkconfig autofs on
chkconfig ypbind on
chkconfig gpm off
chkconfig xmlsysd on
chkconfig kudzu off
alternatives --set mta /usr/sbin/sendmail.postfix

####################################################
# update local config files
####################################################
echo "*.*     @loghost" >>/etc/syslog.conf

cat << EOF >> /etc/yum.conf
[phy-rpms]
Name=Phy 7.3 RPMS
baseurl=http://myserver.mydomain.edu/rh-7.3/phy-rpms/
gpgcheck=0

EOF

# fstab
cat << EOF >> /etc/fstab
einstein:/export/home	/home/einstein nfs     rsize=8192,wsize=8192 0 0 
EOF

# make the serial console work
echo "~~:S:wait:/sbin/sulogin /dev/console" >> /etc/inittab
perl -pi -e "s/agetty ttyS0 9600/agetty ttyS0 115200/" /etc/inittab
echo "ttyS0" /etc/securetty

# grub (no splash, please!)
perl -pi -e "s/^splash/# splash/" /boot/grub/grub.conf

cat /dev/null >/etc/motd

# allow amanda from the backup server as user operator
echo "backup.phy.duke.edu   amanda" >> /var/lib/amanda/.amandahosts

# clean up some bullshit
rm -f /etc/auto.misc
rm -f /usr/lib/desktop-links/redhat.links
rm -rf /usr/lib/desktop-links/ja
rm -rf /etc/skel/Desktop/www.redhat.com

chmod 1777 /xtmp

########################################################
# install extra stuff from rpm
########################################################
yum -d 1 -y install phy-misc postinstall gcc3 gcc3-c++ gcc3-g77


#rm nsswitch.conf so we don't get silly nsswitch timeouts
rm -f /etc/nsswitch.conf

# bringing in the new files from the http server
echo "new files:"
mkdir -p /etc/phy
fix_file /etc/phy/type
fix_file /etc/cron.daily/sysupdate.cron 755
fix_file /etc/cron.hourly/sysupdate.cron 755
fix_file /etc/pam.d/passwd
fix_file /etc/pump.conf
fix_file /etc/ssh/ssh_config
fix_file /etc/ssh/sshd_config
fix_file /etc/ssh/ssh_known_hosts
fix_file /etc/auto.master
fix_file /etc/auto.phy
fix_file /etc/rdate-cron.conf
fix_file /etc/postfix/main.cf
fix_file /etc/postfix/master.cf
fix_file /etc/postfix/aliases
fix_file /var/spool/postfix/etc/resolv.conf
fix_file /usr/local/etc/csh_cshrc
fix_file /usr/local/etc/csh_logout
fix_file /usr/local/etc/csh_login
ln -sf /var/phy/mail /var/spool/mail

echo Kickstarted $hostname $type on: `date`  >> /.kickstart
rpm -qa |sort >> /.kickstart
rpm -Va --nofiles --nomd5 >> /.problems

# this grabs these module jobs to run.
 rm -rf /tmp/${type}-jobs
  mkdir -p /tmp/${type}-jobs
  chmod o-rwx /tmp/${type}-jobs
  cd /tmp/${type}-jobs
  for job in iptables-update.job ypconf-fix.job beo-postfix-domain.job
   do
    /usr/bin/wget -q -nd -nH http://lanserver.mydomain.edu/rh-$rhver/cron-scripts/odd-jobs/$job
    chmod +x /tmp/${type}-jobs/$job
    echo "running odd-job $job"
    /tmp/${type}-jobs/$job
   done
  
rm -rf /tmp/${type}-jobs

# ypconf does this shit grr
rm -f /etc/nsswitch.conf

# make this one be last so the nsswitch doesn't cause nis lookups
fix_file /etc/nsswitch.conf
