#!/bin/ash
# Kiran Kamity, 05/11/2004
# Any Netapp additions to the initial sequence of events that must run on
# the RLM go in this file

case "$1" in

	start|"")

		# Unmounting the old root
		# This saves us a lot of RAM (about 9 MB)
		umount /oldroot
		rmdir /oldroot

		# start the watchdog
		insmod /usr/local/module/luna_wdog.o

		# Determine if DIAG or NORMAL mode
		rm -f /var/run/InTestFixture;
		/usr/local/bin/intestfixture;

		# Create dirs necessary for cron and logrotate
		mkdir -p /var/lib/logrotate;
		mkdir -p /var/spool;

		# Figure out whether we are booted from primary or backup images
		rm -f /var/run/backup_fw
		/usr/local/bin/cpld_reader;

		# Mount jffs2_logs and 
		# Mount jffs2_rootfs - primary and backup
		if [ -f /var/run/backup_fw ]; then
			mount -t jffs2 /dev/mtdblock/1 /mnt/logs;
			mount -t jffs2 /dev/mtdblock/7 /mnt/jffs2_rootfs_backup;
			mount -t jffs2 /dev/mtdblock/2 /mnt/jffs2_rootfs_primary;
		else
			mount -t jffs2 /dev/mtdblock/6 /mnt/logs;
			mount -t jffs2 /dev/mtdblock/2 /mnt/jffs2_rootfs_backup;
			mount -t jffs2 /dev/mtdblock/7 /mnt/jffs2_rootfs_primary;
		fi

		# Creating links to primary and backup metadata info
		mkdir -p /etc/rlm_install;
		ln -sf /mnt/jffs2_rootfs_primary/install /etc/rlm_install/primary;
		ln -sf /mnt/jffs2_rootfs_backup/install /etc/rlm_install/backup;

		if [ -f /var/run/backup_fw ] ; then
	
			# If installing the RLM FW on the backup partition had failed
			# earlier, the env var INSTALLING_BACKUP will be set to 1. 
			# If this is the case, we shouldn't boot from backup. 
			# Sleeping for 10000 seconds (a long time) will let ONTAP reboot
			# us from primary.
	
			installing_backup=`/usr/local/bin/fw_printenv | grep INSTALLING_BACKUP | sed 's/INSTALLING_BACKUP=//g'`;
			if [ "$installing_backup" -eq "1" ] ; then
				sleep 10000;
				# This should never return.
			fi

		else 

			# If installing the RLM FW on the primary partition had failed
			# earlier, the env var INSTALLING_PRIMARY will be set to 1. 
			# If this is the case, we shouldn't boot from primary. 
			# Sleeping for 10000 seconds (a long time) will let ONTAP reboot		
			# us from backup.

			installing_primary=`/usr/local/bin/fw_printenv | grep INSTALLING_PRIMARY | sed 's/INSTALLING_PRIMARY=//g'`;
			if [ "$installing_primary" -eq "1" ] ; then
				sleep 10000;
				# This should never return.
			fi
		fi

		# Start syslogd 
		syslogd -m 0;

		# If the passwdFlag is present, it means that we got rebooted in the midst of updating the 
		# logs partition. In that case, we just use the new password file instead of synchronizing 
		# the old ONTAP users.
		if [ -f /mnt/logs/etc/passwdFlag ] ; then
			mv /mnt/logs/etc/passwd.bak /mnt/logs/etc/passwd
			rm -f /mnt/logs/etc/passwdFlag
		fi

		# Make /etc/passwd a link to the logs partition
		if [ -f /mnt/logs/etc/passwd ] ; then
			# If /mnt/logs/etc/passwd is present, sync ontapusers info from /mnt/logs/etc/passwd to /etc/passwd
			delimit=0;
			touch /etc/passwd.tmp;
			for line in `cat /etc/passwd` ; do
				if [ $delimit -eq 0 ] ; then
					echo $line >> /etc/passwd.tmp;
				fi;
				echo $line | grep -q "#ontapusers";
				if [ $? -eq 0 ]; then
					delimit=1;
				fi
			done;
			delimit=0;
			for line in `cat /mnt/logs/etc/passwd` ; do
				if [ $delimit -eq 1 ] ; then
					echo $line >> /etc/passwd.tmp;
				fi;
				echo $line | grep -q "#ontapusers";
				if [ $? -eq 0 ]; then
					delimit=1;
				fi
			done;
			rm -f /etc/passwd;
			mv /etc/passwd.tmp /etc/passwd;
			rm -f /etc/passwd.tmp;
		fi
		diff /etc/passwd /mnt/logs/etc/passwd;
		# If user credentials haven't changed, no need to write to flash
		# Results of diff: $?=0 if files are same, $?=1 if they differ,
		# $?=2 if any file is missing.
		if ! [ $? = 0 ]; then
			rm -f /mnt/logs/etc/passwd.bak;
			# Save backup copy of merged /etc/passwd file in logs partition. 
			# This copy is used if passwd file in logs partition is corrupted.
			cp -f /etc/passwd /mnt/logs/etc/passwd.bak;
			touch /mnt/logs/etc/passwdFlag
			if [ -f /mnt/logs/etc/rebootFlag ]; then
				rm /mnt/logs/etc/rebootFlag;
				reboot;
			fi
			cp -f /etc/passwd /mnt/logs/etc/passwd
			rm -f /mnt/logs/etc/passwdFlag
		fi
		# Make /etc/passwd a link to /mnt/logs/etc/passwd
		ln -sf /mnt/logs/etc/passwd /etc/passwd

		# Removing the symbolic link to /mnt/logs/etc/ssh
		# to avoid corruption in flash partition
		rm -f /etc/ssh;
		mkdir /etc/ssh;
		# If /mnt/logs/etc/copyStart is present, it means that keys were not 
		# correctly copied to logs partition during previous boot. We should not 
		# copy the keys over to RAM as the files could be corrupt. The corrupt 
		# files should be deleted. However, we should copy the sshd_config as 
		# that file is never touched, and hence cannot be corrupted.
		if ! [ -f /mnt/logs/etc/copyStart ]; then
			cp -a /mnt/logs/etc/ssh/* /etc/ssh;
		else
			rm -f /mnt/logs/etc/ssh/ssh_host_key;
			rm -f /mnt/logs/etc/ssh/ssh_host_key.pub;
			rm -f /mnt/logs/etc/ssh/ssh_host_rsa_key;
			rm -f /mnt/logs/etc/ssh/ssh_host_rsa_key.pub;
			rm -f /mnt/logs/etc/ssh/ssh_host_dsa_key;
			rm -f /mnt/logs/etc/ssh/ssh_host_dsa_key.pub;
			cp -f /mnt/logs/etc/ssh/sshd_config /etc/ssh/sshd_config;
			rm -f /mnt/logs/etc/copyStart;
		fi	

		# Disable ssh root login by default
		sed 's/\(^PermitRootLogin\)\(.*\)\(yes\)/\1\2no/' /etc/ssh/sshd_config > /etc/ssh/sshd_config.new;
		mv /etc/ssh/sshd_config.new /etc/ssh/sshd_config;
		
		# Create a node for /dev/ttyS0 for console pass through
		if ! [ -f /dev/ttyS0 ]; then
			mknod -m 0666 /dev/ttyS0 c 4 64;
		fi

		# Create /var/log/lastlog file to remove err message
		touch /var/log/lastlog;
		chmod 644 /var/log/lastlog;
		
		# bump max number of message queues.  see burt 150196, 279043.
		echo 32 > /proc/sys/kernel/msgmni
		;;

	stop)

		;;

	*)

		echo "Usage: local {start|stop}" >&2
		exit 1
		;;

esac
