#!/bin/sh
#(c) Copyright Barry Kauler, Dec. 2016. Licence: GPL3 (/usr/share/doc/legal).
#161209 simple startup script for Easy Linux. 161212 fixes.
#170713 non-fatal err msgs when run 'reboot' at end this script.
#170727 /sbin/init in main f.s., / no longer remounted rw, so have to do it here.

#this script is in 'easyinit', a cpio archive located at /boot/easyinit
#currently, dec 2016, this is not used as an initramfs, as the kernel boot
#parameter "root=/dev/<system partition>" is direct to a full installation of Easy.
#/sbin/init in the system partition will determine if a filesystem-check, filesystem
#-resize, or recovery (rollback) is needed, then copy 'easyinit' into a ramdisk,
#then pivot_root into it. Hence execution is now here, and the system-partiton
#is still mounted, at /old_root.

#note, the default behaviour of busybox init, when there is no /etc/inittab,
#is to run script /etc/init.d/rcS, which is this one.

#this script is intended as a very basic frontend, if any work is required,
#as itemised above, specialised scripts are called in /sbin

mount -t proc none /proc
mount -t rootfs -o remount,rw rootfs /
ln -s /proc/mounts /etc/mtab 2> /dev/null
touch /etc/fstab #170713 if missing, 'reboot' gives err msg.
PATH="/bin:/sbin"

umount /old_root/dev 2>/dev/null #if kernel configured to mount fs on /dev
#170727 remount /old_root rw...
if [ -f /etc/rc.d/PUPSTATE ];then #file created by /sbin/init in main f.s.
. /etc/rc.d/PUPSTATE #has PDEV1, DEV1FS
 #mount /dev/${PDEV1} -o remount,rw /old_root
 mount -o remount,rw /old_root
 [ -e /old_root/.fsckme.flg ] && rm -f /old_root/.fsckme.flg
 [ -e /old_root/recover.flg ] && rm -f /old_root/recover.flg
 sync
 mount -o remount,ro /old_root
fi

#set the date and time...
HWCLOCKTIME=localtime
[ -s /etc/clock ] && . /etc/clock #has HWCLOCKTIME = localtime or utc
HWDATE="`hwclock --show 2>/dev/null`" #ex: Fri 29 Jun 2012 07:45:28 AM WST  -0.725833 seconds
if [ "${HWDATE}" != "" ];then
 hwclock --hctosys --${HWCLOCKTIME}
else
 if [ -f /old_root/var/local/shutdown_date_saved ];then #see /etc/rc.d/rc.shutdown
  HWDATE="`cat /old_root/var/local/shutdown_date_saved`" #format changed, compatible busybox date, see rc.shutdown.
 else
  #either of these formats can set the date: '29 JUN 2012 10:00:00' '2012-06-28 16:20:08'
  #note, busybox date says '29 JUN 2012 10:00:00' invalid format
  HWDATE="`busybox stat -c %z /old_root/etc/DISTRO_SPECS | cut -f 1 -d '.' | cut -f 1,2 -d ' '`" #creation date of build. ex: '2012-06-28 16:20:08'
 fi
 busybox date -s "${HWDATE}"
fi

#if /etc/rc.d/PUPSTATE exists in easyinit, it got created by /sbin/init in system
#partition, and there are some jobs to do...
#if [ -f /etc/rc.d/PUPSTATE ];then
#. /etc/rc.d/PUPSTATE #has PDEV1, DEV1FS
 if [ "$PDEV1" -a "$DEV1FS" ];then
  [ -f /recover.flg ] && /sbin/rollback /old_root `cat /recover.flg`
  [ -s /.fsckme.flg ] && /sbin/fscheck ${PDEV1} ${DEV1FS} /old_root
 fi
#fi

sync

#161212 tried to do this, get kernel panic "tried to kill init"...
#mount -t devtmpfs devtmpfs /old_root/dev #need to do this before switch_root.
#sync
#umount /proc
#exec switch_root /old_root /sbin/init

echo
echo 'The computer now has to be restarted.'
echo 'Computer will reboot in 2 minutes, else press ENTER for immediate reboot.'
echo "(Developers only: press 'c' then ENTER for shell in ramdisk)"
echo 'NOTE: on some PCs, the keyboard does not work at this stage'
echo '      of bootup, in which case, just wait for the timeout.'
echo
echo -n 'Press ENTER key to reboot:'
read -t 120 abcdef
[ "$abcdef" = "c" ] && exit #go to shell in ramdisk.
#umount /proc #170713 if no /proc, 'reboot' gives err msg.
reboot

###END###
