#!/bin/ash
#(c) Copyright Barry Kauler 2010, bkhome.org
#Lesser GPL license v2 (/usr/share/doc/legal/lgpl-2.1.txt
#note, 3builddistro (in Woof) renames rootfs-skeleton/sbin/initNEW to init.
#perform a f.s. check if file /fsckme.flg exits, otherwise normal startup.
#/etc/rc.d/rc.sysinit creates /fsckme.flg at bootup, rc.shutdown deletes it.
#this script is /sbin/init in the main Puppy f.s., which is the very first to run.
#in the case of not a full hd install, this script just passes execution to busybox init.
#100911 fsckme.err filename changed to fsckme.flg. see also init (initramfs), rc.sysinit, rc.shutdown.
#101020 no longer have /etc/modprobe.conf.
#120408 support minit
#120516 gentoo: libgcc_s.so.1 in wrong place.
#120627 check ramdisk big enough.
#120716 revert 120627, /sys not yet loaded. do it a different way. fix for weird 'tr' missing.
#120716 raspi: no battery-backed hardware clock, so date is wrong, workaround. fixes for rc.sysinit in ramdisk.
#120717 f.s. maximal mount count reached, perform f.s. check.
#120718 try force unmount.
#120729 needed to unmount /old_root/dev, as raspi kernel configured to automount fs on /dev.
#130203 /bin/probedisk2 now /bin/probedisk only.
#130418 support kmod (replaces module-init-tools).
#131207 quirky6: boot param "qfix=fsck", see also rc.sysinit. support f2fs.
#131217 fixed remounting for non-f2fs.
#131225 handle kernel boot param "qfix=bak". refer Quirky Snapshot Manager, in Filesystem menu. 140121 changed "back" to "bak"
#131229 add 'uniq' needed by cmptree. return to ansi default bright white, 0;39m changed to 1;39m.
#131230 recover to use busybox applets only (busybox compiled statically). including run this script with ash. see also /usr/local/petget/removepreview.sh
#140106 add 'realpath' applet, needed by snapshot-manager.
#140106 /fsckme.flg renamed to /.fsckme.flg. see also rc.shutdown, rc.sysinit.
#140122 handle multiarch dir, ex /lib/i386-linux-gnu.
#140211 create cpio of recovery ramdisk.
#140212 static utilities now kept in /audit/reference (ex: 'hwclock' from util-linux). note 'busybox' static has to remain in main f.s.
#140212 fix setting of system date.
#150125 ARCHDIR may be a symlink. L64: amd64 T2 and slackware have /lib64
#150209 guess_fstype was not copied to ramdisk, fixed.
#150303 ${HWDATE} needed $ escaped.
#150929 fsck.f2fs needs /lib/libuuid.so.1
#151106 note, live-cd and frugal, rdev returns nothing.
#151120 quirky werewolf 7.3.x, has /lib64 but no lib*.so* in it.
#151213 no longer hardcode DEV1FS. see also /etc/rc.d/rc.sysinit, /usr/local/install_quirky/install-quirky-to-*
#160406 make failed cp quiet, not an error.
#160408 quirky xerus, libf2fs, libuuid not found.
#160408 warning keyboard may not work during bootup.
#160413 messy, but my 64-bit f2fs-tools pet has /lib/libf2fs.so*

INITEXE='/bin/busybox init'
[ -f /sbin/minit ] && INITEXE='/sbin/minit'

[ -f /etc/rc.d/PUPSTATE ] && . /etc/rc.d/PUPSTATE #151213 2nd+ boots, this exists. want DEV1FS.

#131207 boot parameter...
QFSCK=0; RECOVER=""
[ "$qfix" ] && QFIX="$qfix"
for AFIX in `echo -n "$QFIX" | tr ',' ' '`
do
 case $AFIX in
  fsck|FSCK)  #don't know letters of boot drv (see further down).
   QFSCK=1
  ;;
  back|BACK|bak|BAK) #131225 recovery.
   #want to create a ramdisk and run snapshot-manager in it...
   RECOVER="yes"
  ;;
 esac
done

#131225 in snapshot-manager, if choose a recovery-date, may reboot...
[ -e /recover.flg ] && RECOVER="`cat /recover.flg`" #ex: 201312250830

if [ $QFSCK -eq 0 -a "$RECOVER" = "" ];then
 [ ! -e /.fsckme.flg ] && exec ${INITEXE} #no error.
 PDEV1="`cat /.fsckme.flg | cut -f 1 -d ','`" #file format: sda1,ext3,
 DEV1FS="`cat /.fsckme.flg | cut -f 2 -d ','`"
 CHECK="`cat /.fsckme.flg | cut -f 3 -d ','`" #120717 see rc.shutdown.
fi
[ ! "$DEV1FS" ] && exec ${INITEXE} #151213 full install 1st boot do not have this.

PATH='/bin:/sbin'
export LANG=C

if [ "$RECOVER" = "" ];then #131225
#only come here if previous improper shutdown, or boot req (qfix=fsck).
case $DEV1FS in
 ext2|ext3|ext4|f2fs)
  busybox mount -t proc none /proc
  if [ "$DEV1FS" = "f2fs" ];then #131217
   busybox mount /dev/root -o remount,rw,relatime,background_gc=off /
  else
   busybox mount /dev/root -o remount,rw /
  fi
  echo -e "\\033[1;31m"  >/dev/console #34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black.
  if [ $QFSCK -eq 1 ];then
   #PDEV1="$(df | grep ' /$' | cut -f 1 -d ' ' | cut -f 3 -d '/')"
   PDEV1="`busybox rdev | grep ' /$' | cut -f 1 -d ' ' | grep -E '/dev/sd|/dev/hd|/dev/mmc' | cut -f 3 -d '/'`"
   #151106 note, live-cd and frugal, rdev returns nothing.
   [ ! "$PDEV1" ] && exec ${INITEXE} #sanity check.
   echo "Performing filesystem check of '${PDEV1}', on request" > /dev/console
  else
   if [ "$CHECK" = "MAXIMAL" ];then #120717
    echo "Performing filesystem check of '${PDEV1}', maximal mount count" > /dev/console
   else
    echo "Performing filesystem check of '${PDEV1}' after improper shutdown" > /dev/console
   fi
  fi

  echo -n "NOTE: no progress indication, may take awhile, please wait patiently..."
  echo -e "\\033[1;39m" >/dev/console #131229
  echo  >/dev/console
  rm -f /.fsckme.flg 2>/dev/null
  #creat a ramdisk... 
  #note, Puppy kernels are compiled with 32MB ramdisk, so need to be careful not to fill it up...
  echo 'Creating a ramdisk and populating with a Linux environment...'  >/dev/console
  mkfs.ext2 -m 0 /dev/ram0 > /dev/null 2>&1 #140211
  mkdir -p /mnt/ram0
  busybox mount -t ext2 /dev/ram0 /mnt/ram0
  #populate the ramdisk...
  #140122 multiarch directory...
  ARCHDIR="$(ls -1 /lib/*/libc.so.6 2>/dev/null | cut -f 3 -d '/')" #ex: i386-linux-gnu
  [ "$ARCHDIR" ] && [ -h /lib/${ARCHDIR} ] && ARCHDIR='' #150125 symlink to /lib
  L64='' #150125
  [ -e /lib64 ] && [ ! -h /lib64 ] && L64='64' #150125
  mkdir -p /mnt/ram0/old_root
  mkdir /mnt/ram0/bin
  for ONEBIN in "[" "[[" ash autologinroot basename busybox cat chmod chown clear cp cut date dc dd df dirname dmesg du ed expr false find free grep head kill ln login ls lsmod mkdir mknod more mv nice ntfs-3g pidof pwd readlink rev rm rmdir sed sleep sort stat su sync tail tar touch tr true uname uniq usleep waitmax wc which xargs
  do
   if [ -f /audit/reference/${ONEBIN} ];then #140212 static utility.
    cp -a /audit/reference/${ONEBIN} /mnt/ram0/bin/
    continue
   fi
   if [ -e /bin/$ONEBIN ];then
    cp -a /bin/$ONEBIN /mnt/ram0/bin/
    continue
   fi
   if [ -e /usr/bin/$ONEBIN ];then #150209
    if [ ! -h /usr/bin/$ONEBIN ];then
     cp -a /usr/bin/$ONEBIN /mnt/ram0/bin/
     continue
    fi
   fi
   ln -s busybox /mnt/ram0/bin/$ONEBIN
  done
  ln -snf busybox /mnt/ram0/bin/ps
  ln -snf busybox /mnt/ram0/bin/rdev
  ln -snf busybox /mnt/ram0/bin/sh
  ln -snf busybox /mnt/ram0/bin/mount
  ln -snf busybox /mnt/ram0/bin/umount
  mkdir /mnt/ram0/sbin
  for ONEBIN in depmod dumpe2fs e2fsck elspci fdisk freeramdisk fsck.ext2 fsck.ext3 fsck.ext4 fsck.f2fs getty guess_fstype halt hdparm hotplug2stdout hotplug2stdout_notimeout hwclock ifconfig ifplugstatus insmod klogd kmod ldconfig loadkmap losetup lsmod mingetty mount.ntfs-3g pivot_root probedisk probepart swapon #130418 131207
  do
   if [ -f /audit/reference/${ONEBIN} ];then #140212 static utility.
    cp -a /audit/reference/${ONEBIN} /mnt/ram0/sbin/
    continue
   fi
   if [ -e /sbin/$ONEBIN ];then
    cp -a /sbin/$ONEBIN /mnt/ram0/sbin/
    continue
   fi
   if [ -e /usr/sbin/$ONEBIN ];then #150209
    if [ ! -h /usr/sbin/$ONEBIN ];then
     cp -a /usr/sbin/$ONEBIN /mnt/ram0/sbin/
     continue
    fi
   fi
   ln -s ../bin/busybox /mnt/ram0/sbin/$ONEBIN
  done
  ln -snf ../bin/busybox /sbin/chroot          #must match
  ln -snf ../bin/busybox /mnt/ram0/sbin/chroot #must match
  ln -snf ../bin/busybox /mnt/ram0/sbin/init
  ln -snf ../bin/busybox /mnt/ram0/sbin/poweroff
  ln -snf ../bin/busybox /mnt/ram0/sbin/reboot
  mkdir /mnt/ram0/etc
  #101020 no longer have /etc/modprobe.conf...
  for ONEFILE in codepage DISTRO_SPECS eventmanager fdprm floppy fontmap group gshadow host.conf hostname hosts hosts.allow hosts.deny inittab inoutrc issue keymap localtime mailcap mime.types mke2fs.conf modules.conf mtab networks nscd.conf nsswitch.conf passwd printcap protocols services shadow
  do
   cp -a /etc/$ONEFILE /mnt/ram0/etc/ 2>/dev/null
  done
  cp -a /etc/modprobe.d /mnt/ram0/etc/ 2>/dev/null #101020
  echo '/lib' > /mnt/ram0/etc/ld.so.conf
  [ "$ARCHDIR" ] && echo "/lib/${ARCHDIR}" >> /mnt/ram0/etc/ld.so.conf
  echo 'export PATH="/bin:/sbin"
LD_LIBRARY_PATH="/lib"
ulimit -c 0
umask 022
export USER=root
export LOGNAME=root
export TERM=linux' > /mnt/ram0/etc/profile
  [ "$ARCHDIR" ] && echo "LD_LIBRARY_PATH=/lib:/lib/${ARCHDIR}" >> /mnt/ram0/etc/profile
  echo 'export LD_LIBRARY_PATH' >> /mnt/ram0/etc/profile
  LOCALTIME="/usr/share`readlink /etc/localtime | sed 's%^.*/zoneinfo%/zoneinfo%'`"
  LOCALTDIR="`dirname $LOCALTIME`"
  mkdir -p /mnt/ram0/$LOCALTDIR
  cp -a $LOCALTIME /mnt/ram0/$LOCALTDIR/
  mkdir /mnt/ram0/etc/rc.d
  #120716 raspi: no battery-backed hardware clock, so date is wrong...
  mkdir -p /mnt/ram0/var/local/
  echo "#!/bin/sh
echo 'Now running in ramdisk, performing filesystem check of ${PDEV1}...'
/bin/busybox mount -t proc none /proc
HWDATE=\"\`/sbin/hwclock --show 2>/dev/null\`\" #ex: Fri 29 Jun 2012 07:45:28 AM WST  -0.725833 seconds
if [ \"\${HWDATE}\" != \"\" ];then
 /sbin/hwclock --hctosys --localtime #from util-linux.
else
 if [ -f /old_root/var/local/shutdown_date_saved ];then #see /etc/rc.d/rc.shutdown
  HWDATE=\"\`/bin/cat /old_root/var/local/shutdown_date_saved\`\" #140122 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'
  #120718 busybox date says '29 JUN 2012 10:00:00' invalid format
  HWDATE=\"\`/bin/busybox stat -c %z /old_root/etc/DISTRO_SPECS | /bin/cut -f 1 -d '.' | /bin/cut -f 1,2 -d ' '\`\" #creation date of build. ex: '2012-06-28 16:20:08'
 fi
 /bin/date -s \"\${HWDATE}\"
fi
/bin/busybox umount /old_root/dev 2>/dev/null #if kernel configured to mount fs on /dev
/bin/busybox umount /old_root
if [ \$? -ne 0 ];then
 echo 'Not so good, was unable to unmount ${PDEV1}. Trying forced unmount...'
 /bin/busybox umount -f /old_root #120718 try force unmount.
 if [ \$? -ne 0 ];then
  echo 'Unable to unmount, aborting filesystem check.'
 else
  echo 'Unmount successful, continuing with f.s. check...'
  UMOUNTFLAG='yes'
 fi
else
 UMOUNTFLAG='yes'
fi
if [ \"\$UMOUNTFLAG\" = \"yes\" ];then
 if [ \"${DEV1FS}\" = \"f2fs\" ];then
  /sbin/fsck.f2fs /dev/${PDEV1}
 else
  /sbin/e2fsck -p -f /dev/${PDEV1}
 fi
 /bin/busybox sync
 echo 'Filesystem check completed!'
fi
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.
/bin/busybox umount /proc
/bin/busybox reboot" > /mnt/ram0/etc/rc.d/rc.sysinit
  chmod 755 /mnt/ram0/etc/rc.d/rc.sysinit
  cp -a /dev /mnt/ram0/
  mkdir /mnt/ram0/lib
  [ -e /lib64 ] && ln -s lib /mnt/ram0/lib64 #150125
  cp -a /lib${L64}/lib*.so* /mnt/ram0/lib/ 2> /dev/null #150125. 151120 make quiet.
  cp -a /lib${L64}/ld*.so* /mnt/ram0/lib/ 2> /dev/null #150125. 160406 make quiet.
  cp -a /usr/lib${L64}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #131207. 160408
  cp -a /lib${L64}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #131209. 160408
  cp -a /lib${L64}/libuuid.so* /mnt/ram0/lib/ 2>/dev/null #150929. 160408
  [ ! -e /mnt/ram0/lib/libgcc_s.so.1 ] && cp -a -f --remove-destination /usr/lib${L64}/libgcc_s.so.* /mnt/ram0/lib/ 2>/dev/null #120516 gentoo fix. 150125
  cp -a /lib${L64}/e2initrd_helper /mnt/ram0/lib/ 2>/dev/null #dunno what this is. #150125
  #140122 multiarch dirs...
  if [ "$ARCHDIR" ];then
   mkdir -p /mnt/ram0/lib/${ARCHDIR}
   cp -a /lib/${ARCHDIR}/lib*.so* /mnt/ram0/lib/${ARCHDIR}/ 2>/dev/null #151120 appended .so*
   cp -a /lib/${ARCHDIR}/ld-*.so* /mnt/ram0/lib/${ARCHDIR}/ 2>/dev/null #151120 appended .so*
   cp -a /usr/lib/${ARCHDIR}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #160408
   cp -a /lib/${ARCHDIR}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #160408
   cp -a /lib/${ARCHDIR}/libuuid.so* /mnt/ram0/lib/ 2>/dev/null #160408
  fi
  #160413 messy, but my 64-bit f2fs-tools pet has lib here...
  [ ! -e /mnt/ram0/lib/libf2fs.so.0 ] && cp -a /lib/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #160413
  mkdir /mnt/ram0/tmp
  mkdir -p /mnt/ram0/var/run
  mkdir /mnt/ram0/proc
  mkdir -p /mnt/ram0/usr/share
  cp -a /usr/share/terminfo /mnt/ram0/usr/share/
  #cleanup files...
  rm -f /var/log/X*
  rm -rf /tmp/*
  rm -f /var/lock/LCK*
  rm -f /var/run/*.pid
  rm -rf /root/tmp 2>/dev/null
  rm -rf /root/.thumbnails/* 2>/dev/null
  #120716 weird bug raspi, can't find 'tr', so use 'busybox tr'...
  rm -f `find /var -mount -type s | busybox tr '\n' ' '` #remove sockets.
  rm -f `find /var -mount -type p | busybox tr '\n' ' '` #remove pipes.
  sync
  #pivot_root to the ramdisk...
  echo 'Performing a pivot_root to the ramdisk...'  >/dev/console
  busybox mount /dev/root -o remount,ro /
  busybox umount /proc
  cd /mnt/ram0
  pivot_root . old_root
  exec chroot . sh -c "exec /bin/busybox init" <dev/console >dev/console 2>&1
  #...the ramdisk is setup so that /etc/rc.d/rc.sysinit will run, which does the f.s. check.
 ;;
 *)
  echo -e "\\033[1;31m"  >/dev/console #34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black.
  echo -n "Previous improper shutdown, sorry can only do f.s. check of ext2/3/4,f2fs. Rebooting..." > /dev/console
  echo -e "\\033[1;39m" >/dev/console
  sleep 5
  exec ${INITEXE}
 ;;
esac
fi #131225 end of f.s. check code.

###################################
#131225 only come here to recover...
#140122 copying /lib libs, however, think using all static apps, so libs not needed. ***NOTE***
#repeat most of the above, create a ramdisk, except run snapshot-manager...
[ "$RECOVER" = "yes" ] && RECOVER="" #otherwise, RECOVER will have the rollback-date.
#we want to run this: snapshot-manager /old_root $RECOVER
  busybox mount -t proc none /proc
  if [ "$DEV1FS" = "f2fs" ];then #131217
   busybox mount /dev/root -o remount,rw,relatime,background_gc=off /
  else
   busybox mount /dev/root -o remount,rw,relatime /
  fi
  busybox rm -f /recover.flg 2>/dev/null
  #creat a ramdisk... 
  #note, Puppy kernels are compiled with 32MB ramdisk, so need to be careful not to fill it up...
  echo 'Creating a ramdisk and populating with a Linux environment...'  >/dev/console
  busybox mkfs.ext2 -m 0 /dev/ram0  > /dev/null 2>&1 #140211
  busybox mkdir -p /mnt/ram0
  busybox mount -t ext2 /dev/ram0 /mnt/ram0
  #populate the ramdisk...
  #140122 multiarch directory...
  ARCHDIR="$(busybox ls -1 /lib/*/libc.so.6 2>/dev/null | busybox cut -f 3 -d '/')" #ex: i386-linux-gnu
  [ "$ARCHDIR" ] && [ -h /lib/${ARCHDIR} ] && ARCHDIR='' #150125 symlink to /lib
  L64='' #150125
  [ -e /lib64 ] && [ ! -h /lib64 ] && L64='64' #150125
  busybox mkdir -p /mnt/ram0/old_root
  busybox mkdir /mnt/ram0/bin
  
  busybox cp -a /bin/busybox /mnt/ram0/bin/
  for ONEBIN in "[" "[[" ash awk basename cat chmod chown clear cmp cp cpio cut date dc dd df dirname dmesg du ed expr false find free gettext grep head kill ln login ls lsmod mkdir mknod more mount mv nice pidof ps pwd rdev readlink realpath rev rm rmdir sed sh sleep sort stat su sync tail tar timeout touch tr true umount uname uniq usleep wc which xargs xdelta3 #140106 add realpath. 140212
  do
   if [ -f /audit/reference/${ONEBIN} ];then #140212 static utility.
    busybox cp -a /audit/reference/${ONEBIN} /mnt/ram0/bin/
    continue
   fi
   busybox ln -s busybox /mnt/ram0/bin/$ONEBIN
  done
  #need: autologinroot cp ntfs-3g
  #cp -a -f /bin/cp /mnt/ram0/bin/ #need full version. 140212 now in /audit/reference
  
  busybox mkdir /mnt/ram0/sbin
  for ONEBIN in depmod fdisk freeramdisk getty halt hdparm hwclock ifconfig init insmod klogd loadkmap losetup lsmod pivot_root poweroff reboot swapon #130418 131207
  do
   if [ -f /audit/reference/${ONEBIN} ];then #140212 static utility.
    busybox cp -a /audit/reference/${ONEBIN} /mnt/ram0/sbin/
    continue
   fi
   busybox ln -s ../bin/busybox /mnt/ram0/sbin/$ONEBIN
  done
  #need:  dumpe2fs e2fsck elspci fsck.ext2 fsck.ext3 fsck.ext4 fsck.f2fs guess_fstype hotplug2stdout hotplug2stdout_notimeout ifplugstatus kmod ldconfig mingetty mount.ntfs-3g probedisk probepart
  
  busybox ln -snf ../bin/busybox /sbin/chroot          #must match
  busybox ln -snf ../bin/busybox /mnt/ram0/sbin/chroot #must match
  
  for AMO in `busybox ls -1 /usr/share/locale/*/LC_MESSAGES/snapshot-manager.mo 2>/dev/null | busybox tr '\n' ' '`
  do
   ADIR="`busybox dirname $AMO`"
   busybox mkdir -p /mnt/ram0${ADIR}
   busybox cp -a -f $AMO /mnt/ram0${ADIR}/
  done
  
  #for snapshot-manager, need these...
  #busybox cp -a -f /bin/gettext /mnt/ram0/bin/ #my special compiled statically. 140212
  #busybox cp -a -f /usr/bin/xdelta3 /mnt/ram0/bin/ #my special compiled statically. 140212
  busybox cp -a -f /usr/sbin/snapshot-manager /mnt/ram0/sbin/
  busybox cp -a -f /usr/bin/cmptree /mnt/ram0/bin/ #131227
  busybox cp -a -f /sbin/mingetty /mnt/ram0/sbin/ #needed for login, see inittab. compiled statically. 150209 not static
  [ -f /sbin/guess_fstype ] && busybox cp -a -f /sbin/guess_fstype /mnt/ram0/sbin/ #static. 150209
  [ -f /usr/sbin/guess_fstype ] && busybox cp -a -f /usr/sbin/guess_fstype /mnt/ram0/sbin/ #static. 150209 not static
  
  busybox mkdir /mnt/ram0/etc
  for ONEFILE in codepage DISTRO_SPECS eventmanager fdprm floppy fontmap group gshadow host.conf hostname hosts hosts.allow hosts.deny inittab inoutrc issue keymap localtime mailcap mime.types mke2fs.conf modules.conf mtab networks nscd.conf nsswitch.conf passwd printcap protocols services shadow
  do
   busybox cp -a /etc/$ONEFILE /mnt/ram0/etc/ 2>/dev/null
  done
  busybox cp -a /etc/modprobe.d /mnt/ram0/etc/ 2>/dev/null #101020
  echo '/lib' > /mnt/ram0/etc/ld.so.conf
  [ "$ARCHDIR" ] && echo "/lib/${ARCHDIR}" >> /mnt/ram0/etc/ld.so.conf
  echo 'export PATH="/bin:/sbin"
LD_LIBRARY_PATH="/lib"
ulimit -c 0
umask 022
export USER=root
export LOGNAME=root
export TERM=linux' > /mnt/ram0/etc/profile
  [ "$ARCHDIR" ] && echo "LD_LIBRARY_PATH=/lib:/lib/${ARCHDIR}" >> /mnt/ram0/etc/profile
  echo 'export LD_LIBRARY_PATH' >> /mnt/ram0/etc/profile
  LOCALTIME="/usr/share`busybox readlink /etc/localtime | busybox sed 's%^.*/zoneinfo%/zoneinfo%'`"
  LOCALTDIR="`dirname $LOCALTIME`"
  busybox mkdir -p /mnt/ram0/$LOCALTDIR
  busybox cp -a $LOCALTIME /mnt/ram0/$LOCALTDIR/
  busybox mkdir /mnt/ram0/etc/rc.d
  #120716 raspi: no battery-backed hardware clock, so date is wrong...
  busybox mkdir -p /mnt/ram0/var/local/
  echo "#!/bin/sh
echo 'Now running in ramdisk, performing system recovery...'
/bin/busybox mount -t proc none /proc
HWDATE=\"\`/sbin/hwclock --show 2>/dev/null\`\" #ex: Fri 29 Jun 2012 07:45:28 AM WST  -0.725833 seconds
if [ \"\${HWDATE}\" != \"\" ];then
 /sbin/hwclock --hctosys --localtime #from util-linux. note, busybox hwclock is broken.
else
 if [ -f /old_root/var/local/shutdown_date_saved ];then #see /etc/rc.d/rc.shutdown
  HWDATE=\"\`/bin/cat /old_root/var/local/shutdown_date_saved\`\" #140122 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'
  #120718 busybox date says '29 JUN 2012 10:00:00' invalid format
  HWDATE=\"\`/bin/busybox stat -c %z /old_root/etc/DISTRO_SPECS | /bin/cut -f 1 -d '.' | /bin/cut -f 1,2 -d ' '\`\" #creation date of build. ex: '2012-06-28 16:20:08'
 fi
 /bin/date -s \"\${HWDATE}\"
fi
/bin/busybox umount /old_root/dev 2>/dev/null #if kernel configured to mount fs on /dev
###RECOVER###
snapshot-manager /old_root ${RECOVER}
sync
/bin/busybox umount /old_root
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.
/bin/busybox umount /proc
/bin/busybox reboot" > /mnt/ram0/etc/rc.d/rc.sysinit
  busybox chmod 755 /mnt/ram0/etc/rc.d/rc.sysinit
  busybox cp -a /dev /mnt/ram0/
  busybox mkdir /mnt/ram0/lib
  [ -e /lib64 ] && ln -s lib /mnt/ram0/lib64 #150125
  busybox cp -a /lib${L64}/lib*.so* /mnt/ram0/lib/ 2> /dev/null #150125. 151120 make quiet.
  busybox cp -a /lib${L64}/ld*.so* /mnt/ram0/lib/ 2> /dev/null #150125. 160406 make quiet.
  busybox cp -a /usr/lib${L64}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #131207. 160408
  busybox cp -a /lib${L64}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #131209. 160408
  busybox cp -a /lib${L64}/libuuid.so* /mnt/ram0/lib/ 2>/dev/null #150929. 160408
  [ ! -e /mnt/ram0/lib/libgcc_s.so.1 ] && busybox cp -a -f /usr/lib${L64}/libgcc_s.so.* /mnt/ram0/lib/ 2>/dev/null #120516 gentoo fix. 150125
  busybox cp -a /lib${L64}/e2initrd_helper /mnt/ram0/lib/ 2>/dev/null #dunno what this is. 150125
  #140122 multiarch dirs...
  if [ "$ARCHDIR" ];then
   busybox mkdir -p /mnt/ram0/lib/${ARCHDIR}
   busybox cp -a /lib/${ARCHDIR}/lib*.so* /mnt/ram0/lib/${ARCHDIR}/ 2>/dev/null #151120 appended .so*
   busybox cp -a /lib/${ARCHDIR}/ld-*.so* /mnt/ram0/lib/${ARCHDIR}/ 2>/dev/null #151120 appended .so*
   cp -a /usr/lib/${ARCHDIR}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #160408
   cp -a /lib/${ARCHDIR}/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #160408
   cp -a /lib/${ARCHDIR}/libuuid.so* /mnt/ram0/lib/ 2>/dev/null #160408
  fi
  #160413 messy, but my 64-bit f2fs-tools pet has lib here...
  [ ! -e /mnt/ram0/lib/libf2fs.so.0 ] && cp -a /lib/libf2fs.so* /mnt/ram0/lib/ 2>/dev/null #160413
  busybox mkdir /mnt/ram0/tmp
  busybox mkdir -p /mnt/ram0/var/run
  busybox mkdir /mnt/ram0/proc
  busybox mkdir -p /mnt/ram0/usr/share
  busybox cp -a /usr/share/terminfo /mnt/ram0/usr/share/
  #cleanup files...
  busybox rm -f /var/log/X*
  busybox rm -rf /tmp/*
  busybox rm -f /var/lock/LCK*
  busybox rm -f /var/run/*.pid
  busybox rm -rf /root/tmp 2>/dev/null
  busybox rm -rf /root/.thumbnails/* 2>/dev/null
  #120716 weird bug raspi, can't find 'tr', so use 'busybox tr'...
  busybox rm -f `busybox find /var -xdev -type s | busybox tr '\n' ' '` #remove sockets.
  busybox rm -f `busybox find /var -xdev -type p | busybox tr '\n' ' '` #remove pipes.
  busybox sync
  #pivot_root to the ramdisk...
  echo 'Performing a pivot_root to the ramdisk...'  >/dev/console
  #busybox mount /dev/root -o remount,ro /
  busybox umount /proc
  cd /mnt/ram0
  if [ ! -f /audit/reference/ramdisk-recover.cpio.gz ];then #140211
   busybox find . -mindepth 1 -xdev | busybox cpio -o -H newc | busybox gzip -c > /audit/reference/ramdisk-recover.cpio.gz
   busybox sync
  fi
  busybox pivot_root . old_root
  exec chroot . sh -c "exec /bin/busybox init" <dev/console >dev/console 2>&1
  #...the ramdisk is setup so that /etc/rc.d/rc.sysinit will run, which does the recovery.


###END###
