#!/bin/bash


# first make /tmp to store the log of this file here only for one hour
ROOT_UID=0         # Root has $UID 0.
if [ "$UID" -ne "$ROOT_UID" ]
then
  echo; echo "Only root can run this script."; echo
  exit 65
fi

echo >"/var/log/SysRam.log"

tme=`date`
echo "Moving /tmp, /etc and /var to RAM-Disks at $tme"
echo "Details in /var/log/SysRam.log"
echo -e "Installing and moving /tmp, /etc and /var \nto RAM-Disks at $tme.. \n\n" >>"/var/log/SysRam.log"
set HD = `df /`
(( HDSIZE = ${11} ))

mkdir -p /rTmp
mount -t tmpfs -o mode=1777 rTmp /rTmp
(( MNT = $? ))
# echo "mount: $MNT"
set RAM = `df /rTmp`
(( DIFF = ( $HDSIZE - ${11})/1000 ))
#echo "/tmp:   Mount: $MNT;  Differenz:  $DIFF MB"
echo -n "/tmp: ."

if [ $MNT -eq 0 -a $DIFF -gt 0 ];
then
   MTAB=`mount -v --bind /rTmp /tmp`
	(( TMP = $? ))
	if [ $TMP -eq 0 ];
	then
		echo "ok:      $MTAB, copy CLEAN-tmp.."  >>"/var/log/SysRam.log"
		cp /etc/cron.daily/suse.de-clean-tmp /etc/cron.hourly/suse.de-clean-tmp

		(( CLN = $? ))
		if [ $CLN -eq 0 ];
		then
			echo "ok:      CLEAN-tmp copied to hourly, /etc.."  >>"/var/log/SysRam.log"
			echo -n "."
		else
			echo "UUPS:    CLEAN-tmp was NOT copied to hourly, /etc.."  >>"/var/log/SysRam.log"
			echo -n "#"
		fi
	else
		echo "UUPS:    RAM-/rTmp was NOT bound over /tmp, /etc.."  >>"/var/log/SysRam.log"
		echo -n "#"
	fi

else
   TMP = 9
   echo "UUPS:    RAM for /tmp was  NOT  created, /etc.."  >>"/var/log/SysRam.log"
	echo -n "#"
fi
echo " done"

# copy dirs to /rEtc
mkdir -p /rEtc
mount -t tmpfs -o mode=1755 rEtc /rEtc
(( MNT = $? ))
set RAM = `df /rEtc`
(( DIFF = ( $HDSIZE - ${11})/1000 ))
echo -n "/etc: ."
if [ $MNT -eq 0 -a $DIFF -gt 0 ];
then
	echo "ok:      RAM-/rEtc created, delete CLEAn-tmp in hourly .."  >>"/var/log/SysRam.log"
	echo -n "."
		if [ -e /etc/cron.hourly/suse.de-clean-tmp ];
		then
			UNLK=`unlink /etc/cron.hourly/suse.de-clean-tmp`
			echo "ok:      CLEAN-tmp in /etc/..hourly deleted, copy /etc.."  >>"/var/log/SysRam.log"
			echo -n "."
		fi
	cd /etc
	BLKS=`find . -depth -print0 | cpio -p0md /rEtc 2>&1`
	echo "ok:      $BLKS Blocks copied from /etc to RAM, bind.."  >>"/var/log/SysRam.log"
	echo -n "."
	MTAB=`mount -v --bind /rEtc /etc`
	(( ETC = $? ))
	if [ $ETC -eq 0 ];
	then
		echo "ok:      $MTAB, move CLEAN-tmp.."  >>"/var/log/SysRam.log"
		echo -n "."
		mv /etc/cron.daily/suse.de-clean-tmp /etc/cron.hourly/suse.de-clean-tmp
		(( CLN = $? ))
		if [ $CLN -eq 0 ];
		then
			echo "ok:      CLEAN-tmp moved to hourly, /var.."  >>"/var/log/SysRam.log"
			echo -n "."
		else
			echo "UUPS:    CLEAN-tmp was NOT moved to hourly, /var.."  >>"/var/log/SysRam.log"
			echo -n "#"
		fi
	else
		echo "UUPS:    RAM-/rEtc was NOT bound over /etc, /var.."  >>"/var/log/SysRam.log"
		echo -n "#"
	fi
else
   ETC=9
   echo "UUPS:    RAM for /etc was  NOT  created, /var.."  >>"/var/log/SysRam.log"
	echo -n "#"
fi
echo " done"

# /var:
mkdir -p /rVar
mount -t tmpfs -o mode=1755 rVar /rVar
(( MNT = $? ))
set RAM = `df /rVar`
(( DIFF = ( $HDSIZE - ${11})/1000 ))
echo -n "/var: ."
#echo "/var:  Mount: $MNT;  Differenz:  $DIFF MB"
if [ $MNT -eq 0 -a $DIFF -gt 0 ];
then
	echo "ok:      RAM-/rVar created, copy /var.."  >>"/var/log/SysRam.log"
	echo -n "."
   cd /var
   BLKS=`find . -not \( -regex ".+\.gz" -o  -regex ".*YaST.*"  -o -regex ".*log\/boot.*"  \) -print0 | cpio -p0md /rVar 2>&1`
	echo "ok:      $BLKS Blocks copied from /var to RAM, bind.."  >>"/var/log/SysRam.log"
	echo -n "."
   MTAB=`mount -v --bind /rVar /var`
	(( VAR = $? ))
	if [ $VAR -eq 0 ];
	then
		echo "ok:      $MTAB, logrotate.."  >>"/var/log/SysRam.log"
		echo -n "."
	else
		echo "UUPS:    RAM-/rVar was NOT bound over /var, logrotate.."  >>"/var/log/SysRam.log"
		echo -n "#"
	fi
else
   VAR=9
   echo "UUPS:    RAM for /var was  NOT  created, logrotate.."  >>"/var/log/SysRam.log"
	echo -n "#"
fi
echo " done"

echo -n "rmSysRam-Daemon.. "
nohup /home/SysRam/rmSysRam >>/var/log/SysRam.log &
if [ $? -eq 0 ];
then
	sleep 1
	echo  "started pid: " `less /var/run/SysRam.pid`
else
   echo  "uuups: not started: $?"
fi
echo -n "adjusting: "
#check
if [ $ETC -eq 0 -a $VAR -eq 0 ];
then
	# now adjust /etc..
	# use the predefined ram.cron in sysconfig/:
	mv /rEtc/sysconfig/ram.cron /rEtc/sysconfig/cron
	echo -n "cron, "
	# use the predefined ram.logrotate.conf:
	mv /rEtc/ram.logrotate.conf /rEtc/logrotate.conf
	echo -n "logrotate:."
	# use the predefined ram.* in logrotate.d/:
	mv /rEtc/logrotate.d/ram.xntp /rEtc/logrotate.d/xntp
	echo -n "."
	mv /rEtc/logrotate.d/ram.wtmp /rEtc/logrotate.d/wtmp
	echo -n "."
	mv /rEtc/logrotate.d/ram.syslog /rEtc/logrotate.d/syslog
	echo -n "."
	mv /rEtc/logrotate.d/ram.scpm /rEtc/logrotate.d/scpm
	echo ". done"
	echo "ok:      adjustments made in /etc: cron, xntp, wtmp, syslog, scpm"  >>"/var/log/SysRam.log"
fi

pid=`less /var/run/SysRam.pid`
echo -e "ok:      SysRam-Disks installed, rmSysRam-Daemon runs at pid: $pid\n"  >>"/var/log/SysRam.log"
echo -n "restart services from init 3: "

cd /etc/init.d/rc3.d/
for file in $( find . -type l -name '*' | sort )
do
	KS=${file:2:1}
	name=${file:5}
	# exclude from restart:
	if [ $name = 'network' -o $name = 'nfsserver' -o $name = 'random' -o $name = 'alsasound' -o $name = 'nfslock' -o $name = 'portmap' -o $name = 'sshd' -o $name = 'random' ];
	then
		continue
	fi
	# use Start-Sequence for restart Sequence
	if [ $KS = 'S' ];
	then
		echo -e "\n$name \t restart .."  >>"/var/log/SysRam.log"
		/etc/init.d/./$name restart >>"/var/log/SysRam.log" 2>&1
		echo -n "."
		# sleep 1
	else
		continue
	fi
done
echo " done,  check: (df /etc /var /tmp):"

#check
chk=`df /etc /var /tmp`
echo -e "$chk \n \n#########  ready  ##########"  >>"/var/log/SysRam.log"
echo -e "\n\n/tmp /etc /var moved to ram-disks:\n\n$chk\n\nto remove use: delSysRam" >"/dev/tty1"
echo "$chk"
echo
exit 0


