#!/bin/bash
# $Header: /global/src/etc/init.d/common/RCS/cl-fix,v 1.24 2024/12/03 02:46:28 mas90 Exp $
### BEGIN INIT INFO
# Provides:          cl-wol-g
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: run any required fixes
# Description:       run any required fixes
### END INIT INFO

case "$(hostname)" in
svr-acs-*|acs-[01]*|acs-2[012])	dev=/dev/sda3 n=2100000 # Want 8728648 4k blocks / 8.4G (-h) / 9G (-H)
	case "$(tune2fs -l $dev|grep Block.count)" in *$n*) : OK;; *) (date; df -lh /; resize2fs $dev $n; echo Resize gave $?; df -lh /; echo) >> /RESIZE-FS;; esac
	f=/etc/user-config/patches; grep mphil $f || echo @F-mphil >> $f
esac

# xscreensaver burns CPU cycles and keeps a machine "not idle"
if	test -e /usr/bin/xscreensaver
then	if	grep ^xscreensaver /etc/user-config/bundles > /dev/null
	then	: User wants it
	else	apt-get remove -y -o DPkg::Options::=--force-confdef xscreensaver
	fi
fi

# 2011/12 allow COs to "cl-asuser" having been removed from LDAP group staff revealing group wheel
dir=/etc/user-config/
if getent group wheel > /dev/null && test -d $dir/.
then
	find $dir -group wheel -exec chgrp sysadmin {} \; -ls
fi

# 2022/07/28 UIS are removing the csx.cam.ac.uk, so repo URL has to be changed
grep ' http://www-uxsup.csx.cam.ac.uk/pub/linux/ubuntu\(/*\) ' /etc/apt/sources.list && cl-hostid-fix --do_aptsrc

# 2022/08/25 backstop test to check that ~root/.ssh/
(f=~root/.ssh/authorized_keys; F=$f-cl-samp; test $F -nt $f && cat $F | tee -a $f) || true

# 2024/11/21 fix hardcoded dev-gpu homedir server
if grep -q '^tuffi:/mnt/tuffi/userdata' /etc/fstab && [ -d /home ]
then
	umount -fl /home
	sed -i '/^tuffi:\/mnt\/tuffi\/userdata/ s%tuffi:/mnt/tuffi/userdata%/auto/anfs/gpucluster%;s%\bnfs.*%bind bind,_netdev,nofail,x-systemd.after=autofs.service 0 0%' /etc/fstab
	systemctl daemon-reload
	mount /home
elif grep -q '^/auto/anfs/gpucluster.*bind bind ' /etc/fstab
then
	sed -ri 's%^(/auto/anfs/gpucluster.*bind bind) %\1,_netdev,nofail,x-systemd.after=autofs.service %' /etc/fstab
	systemctl daemon-reload
	umount /home
	mount /home
elif grep -q '^/auto/anfs/gpucluster.*bind bind,x-systemd\.after=autofs\.service ' /etc/fstab
then
	sed -ri 's%^(/auto/anfs/gpucluster.*bind bind)%\1,\_netdev,nofail%' /etc/fstab
	systemctl daemon-reload
fi

if [ -x /usr/groups/linux/cl-fix2.sh ]
then
	/usr/groups/linux/cl-fix2.sh
fi
