#!/bin/sh
# Do the main installation first
/bin/sh /tmp/install

# Now customize it for 6110
#
# Install the grub boot to the first sector of sda3
#
/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
echo "(hd0)      /dev/sda" > /boot/grub2/device.map
/sbin/grub2-install --force /dev/sda3

# Set the machine to reboot into Windows at 8PM
echo "55 7 * * 1,2,3,4,5 /bin/sh /opt/bin/winboot.sh" > /var/spool/cron/root
# Set the default realm to CSSD's
mv /etc/krb5.conf /etc/krb5.conf.cs
cp /etc/krb5.conf.cs /etc/krb5.conf
ed - /etc/krb5.conf << \EOFEOF
/^ default_realm/s/DEPT\.CS\.PITT\.EDU/UNIV.PITT.EDU/
w
q
EOFEOF
#
echo "pitt.edu" > /usr/vice/etc/ThisCell

# Get the HTCondor signing key
wget -O /tmp/RPM-GPG-KEY-HTCondor http://research.cs.wisc.edu/htcondor/yum/RPM-GPG-KEY-HTCondor
rpm --import /tmp/RPM-GPG-KEY-HTCondor
#
yum -y install samba samba-winbind ntfs-3g condor
mkdir /ntfs
#
echo "Swapping new files for old files"
for i in /etc/samba/smb.conf
do
	if [ -f $i.new ]; then
		if [ -f $i ]; then
			mv -i $i $i.orig
		fi
		mv -i $i.new $i
		echo $i
	fi
done
echo " ... done"
#
echo "Setting hardware clock to use local time"
/sbin/hwclock --systohc --localtime
#
echo "Modifying nsswitch and pam to use winbind"
mv /etc/nsswitch.conf /etc/nsswitch.conf.cs
cp /etc/nsswitch.conf.cs /etc/nsswitch.conf
ed - /etc/nsswitch.conf << \EOFEOF
/^passwd:/s/files .*/files winbind/
/^group:/s/files .*/files winbind/
w
q
EOFEOF
#
mv /etc/pam.d/system-auth-ac /etc/pam.d/system-auth-ac.cs
cp /etc/pam.d/system-auth-ac.cs /etc/pam.d/system-auth-ac
ed - /etc/pam.d/system-auth-ac << \EOFEOF
/^auth.*krb5/s/.*/&\
@&/
/^account.*krb5/s/.*/&\
@&/
/^password.*krb5/s/.*/&\
@&/
1,$g/^@/s/krb5/winbind/
1,$s/^@//
$a
session     optional      pam_oddjob_mkhomedir.so
.
w
q
EOFEOF
mv /etc/pam.d/password-auth-ac /etc/pam.d/password-auth-ac.cs
cp /etc/pam.d/system-auth-ac /etc/pam.d/password-auth-ac
#
echo "Modifying oddjobd-mkhomedir conf"
mv /etc/oddjobd.conf.d/oddjobd-mkhomedir.conf /etc/oddjobd.conf.d/oddjobd-mkhomedir.conf.orig
cp /etc/oddjobd.conf.d/oddjobd-mkhomedir.conf.orig /etc/oddjobd.conf.d/oddjobd-mkhomedir.conf
ed - /etc/oddjobd.conf.d/oddjobd-mkhomedir.conf << \EOFEOF
1,$s/\/usr\/libexec\/oddjob\/mkhomedir/& -u 0077/
w
q
EOFEOF
#
mkdir /home/PITT
chmod 711 /home/PITT
#
systemctl enable oddjobd.service
systemctl enable winbind.service
systemctl enable smb.service
systemctl disable ypbind.service

# Installing CUDA
yum -y install /tmp/cuda-repo-rhel7-7-0-local-7.0-28.x86_64.rpm
mkdir /opt/cuda-7.0
# Hack to get around AFS not being up yet...
mv /usr/local /usr/local.afs
mkdir /usr/local
ln -s /opt/cuda-7.0 /usr/local/cuda-7.0
yum -y install cuda
rm -r /usr/local
mv /usr/local.afs /usr/local

echo Installing Eclipse
cd /opt
gunzip -c /tmp/eclipse-java-mars-R-linux-gtk-x86_64.tar.gz |tar xf -
ln -s /opt/eclipse/eclipse /usr/bin/eclipse
cd -

# Disable the nouveau driver so that the nVidia driver can be installed
echo "blacklist nouveau" > /etc/modprobe.d/blacklist-nouveau.conf

# Remove all printers except for ps6110 and ps6n
mv /etc/cups/printers.conf /etc/cups/printers.conf.cs
sed -e "/<Printer undef_printer>/,/<Printer ps6110>/c/<Printer ps6110>/" \
    -e "/<Printer ps6113>/,/<Printer ps6n>/c/<Printer ps6n>/" \
    -e '/<Printer ps6w>/,$d' \
    /etc/cups/printers.conf.cs > /etc/cups/printers.conf

echo "ach54	ALL=(ALL)	ALL" >> /etc/sudoers.d/techstaff
echo "wsg4	ALL=(ALL)	ALL" >> /etc/sudoers.d/techstaff

# Set /etc/rc.local to run the /tmp/firstboot script and then revert
# back to the original script
RCLOCAL=/etc/rc.d/rc.local
mv ${RCLOCAL} ${RCLOCAL}.orig
cp ${RCLOCAL}.orig ${RCLOCAL}
echo "sh /tmp/firstboot > /var/log/firstboot.log 2>&1" >> ${RCLOCAL}
echo "rm /tmp/firstboot" >> ${RCLOCAL}
echo "chmod 700 /var/log/firstboot.log" >> ${RCLOCAL}
echo "mv ${RCLOCAL} ${RCLOCAL}.tmp" >> ${RCLOCAL}
echo "mv ${RCLOCAL}.orig ${RCLOCAL}" >> ${RCLOCAL}
echo "rm ${RCLOCAL}.tmp" >> ${RCLOCAL}
chmod 755 ${RCLOCAL}
