#! /bin/sh
echo ""
echo "Beginning local customization"
cd /
#
KEY=/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-8
if [ -f ${KEY} ]; then
	echo -n "Importing CentOS RPM-GPG-KEY ... "
	rpm --import ${KEY}
	echo "done"
else
	echo "${KEY} does not exist!"
fi
#

KEY=/etc/pki/rpm-gpg/SCI-RPM-KEY
wget -O ${KEY} http://install.cs.pitt.edu/centos8/openafs/SCI-RPM-KEY
if [ -f ${KEY} ]; then
	echo -n "Importing SCI OpenAFS RPM key ... "
	rpm --import ${KEY}
	echo "done"
else
	echo "${KEY} does not exist!"
fi
#

REPOS=/etc/yum.repos.d
for DIST in Base AppStream PowerTools
do
    if [ -f ${REPOS}/CentOS-${DIST}.repo ]; then
        mv ${REPOS}/CentOS-${DIST}.repo ${REPOS}/CentOS-${DIST}.repo.orig
        sed -e "s/^mirrorlist/#&/" -e "s/^#baseurl/baseurl/" \
          -e "/^baseurl/s/mirror.centos.org/mirror.cs.pitt.edu/" \
          -e "/^enabled=0/enabled=1/" \
          ${REPOS}/CentOS-${DIST}.repo.orig > ${REPOS}/CentOS-${DIST}.repo
    fi
done

echo "Installing updates ... "
/usr/bin/yum -y update
if [ $? -ne 0 ]; then
    echo "FAILED!  Remainder of customizations aborted."
    exit 1
else
    echo "done"
fi

# Install EPEL, then DKMS, then OpenAFS, then pam_afs_session
yum -y install epel-release
yum -y install dkms
yum -y install openafs openafs-client openafs-compat openafs-krb5 dkms-openafs
# pam_afs_session comes from epel; not available for centos 8 yet
# We made our own and it's in the SCI repository
yum -y install pam_afs_session

# these aren't available for 8 yet:  kstart taskjuggler* unique* xfig*
# Install extra packages from the EPEL and SCI repositories
yum -y install kstart sshpass tofrodos

# Configure OpenAFS
mv /usr/vice/etc/ThisCell /usr/vice/etc/ThisCell.orig
echo "cs.pitt.edu" > /usr/vice/etc/ThisCell
echo "cs.pitt.edu cs" > /usr/vice/etc/CellAlias
ed - /usr/vice/etc/cacheinfo << \EOFEOF
s/\/usr\/vice\/cache/\/var\/vice\/cache/
w
q
EOFEOF
mkdir -p /var/vice/cache
chmod 700 /var/vice/cache
ed - /usr/lib/systemd/system/openafs-client.service << \EOFEOF
/^ExecStart=/a
ExecStartPost=/usr/bin/fs sysname -newsys amd64_cen7
ExecStartPost=/usr/bin/fs flushvolume /afs/cs.pitt.edu/system/@sys
.
w
q
EOFEOF
systemctl daemon-reload
# need to add empty files if none exist
for i in /usr/vice/etc/SuidCells.dist /usr/vice/etc/SuidCells.local
do
    if [ ! -f "$i" ]; then
        cp /dev/null $i
    fi
done
systemctl enable openafs-client.service

echo "Swapping new files for old files"
for i in etc/authselect/password-auth \
	etc/authselect/system-auth \
	etc/chrony.conf \
	etc/cups/printers.conf \
	etc/dconf/db/gdm.d/00-login-screen \
	etc/dconf/db/gdm.d/01-banner-message \
	etc/dconf/db/gdm.d/01-logo \
	etc/dconf/db/gdm.d/02-disable-user-switching \
	etc/issue.net \
	etc/issue \
	etc/krb5.conf \
	etc/postfix/main.cf \
	etc/snmp/snmpd.conf \
	etc/ssh/sshd_config \
	root/.bashrc
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
# Update the dconf database so the new config files take effect
dconf update
#
echo " ... done"
#
mkdir /var/run/authdir
#

# If NIS is installed, make sure both servers are listed
if [ -f /etc/yp.conf ]; then
    echo "domain cs server lisa.cs.pitt.edu" >> /etc/yp.conf
    echo "domain cs server maggie.cs.pitt.edu" >> /etc/yp.conf
    systemctl enable ypbind.service
    /bin/mv /etc/hosts /etc/hosts.orig
    /bin/cp /etc/hosts.orig /etc/hosts
    echo "" >> /etc/hosts
    echo "136.142.23.69    lisa.cs.pitt.edu lisa" >> /etc/hosts
    echo "136.142.23.70    maggie.cs.pitt.edu maggie" >> /etc/hosts
fi
#
SELINUX=`grep SELINUX=enforcing /etc/selinux/config`
if [ "${SELINUX}x" != "x" ]; then
    echo -n "Disabling SELinux ..."
    mv /etc/selinux/config /etc/selinux/config.orig
    sed -e "s/^SELINUX=enforcing/SELINUX=disabled/" \
        /etc/selinux/config.orig > /etc/selinux/config
    echo " ... done"
fi
#
if [ -f /etc/Muttrc ]; then
    echo -n "Modifying /etc/Muttrc ..."
    mv /etc/Muttrc /etc/Muttrc.orig
    sed -e '/^# set hidden_host=no/s//set hidden_host=yes/' \
        -e '/^# set spoolfile=""/s//set spoolfile="{outlook.office365.com}inbox"/' \
        /etc/Muttrc.orig >/etc/Muttrc
    echo " ... done"
else
    echo /etc/Muttrc not found
fi
#
echo "*.*							@logger" > /etc/rsyslog.d/catchall.conf
#
echo -n "Configuring boot-time services ... "
systemctl enable NetworkManager-wait-online.service
systemctl enable snmpd.service
systemctl disable firewalld.service
systemctl disable kdump.service
systemctl disable libvirtd
systemctl disable libvirt-guests
systemctl disable virt-who
echo "done"
#
echo -n "Making symbolic links ... "
mv /usr/local /usr/local.orig
# @sys may be broken in CentOS 8 (was broken in CentOS 7); hard-code the link
#ln -s /afs/cs.pitt.edu/system/@sys/usr/local /usr/local
ln -s /afs/cs.pitt.edu/system/amd64_cen8/usr/local /usr/local
ln -s /afs/cs.pitt.edu/system/share/etc/nightly_update /etc/cron.daily/nightly_update
echo "done"
#

# I'm not sure if this is still supported in CentOS 8 or not but
# it won't hurt.
for i in `ls /etc/sysconfig/network-scripts/ifcfg-* |sed -e /ifcfg-lo/d`
do grep "BOOTPROTO=\"*dhcp\"*" $i > /dev/null
    if [ $? -eq 0 ]; then
        grep "ONBOOT=\"*yes\"*" $i > /dev/null
        if [ $? -eq 0 ]; then
            grep "PERSISTENT_DHCLIENT=\"*yes\"*" $i > /dev/null
            if [ $? -ne 0 ]; then
                echo "PERSISTENT_DHCLIENT=\"yes\"" >> $i
            fi
        fi
    fi
done

cat > /etc/sudoers.d/techstaff << \EOFEOF
hoffman    ALL=(ALL)       ALL
n3cvl      ALL=(ALL)       ALL
ahobaugh   ALL=(ALL)       ALL
ach54      ALL=(ALL)       ALL
mad371     ALL=(ALL)       ALL
EOFEOF
chmod 440 /etc/sudoers.d/techstaff

mv /etc/sysconfig/man-db /etc/sysconfig/man-db.orig
sed -e "/OPTS/s/-q/& \/usr\/share\/man/" /etc/sysconfig/man-db.orig > /etc/sysconfig/man-db

if [ -f /var/yp/nicknames ]; then
    /bin/mv /var/yp/nicknames /var/yp/nicknames.orig
    /bin/cp /var/yp/nicknames.orig /var/yp/nicknames
    echo "shadow            shadow.byname" >> /var/yp/nicknames
fi

#
echo ""
echo ""
echo "All done.  Cross your fingers and reboot."
