#!/bin/sh
#called from menu "Shutdown -> Reboot to initrd (developers only)".
#ref: /etc/xdg/templates/_root_.jmwrc and init in initrd.
#200802 first release.
#200806 put .debug.flg in boot-partition, so can see it when booted to zram.

#we have kernel boot parameter "qfix=dropout<n>" where "<n>" is a number between
#0 and 6. These are different droput places in the init script in the initrd.
#dropout is to a console. "exit" from console, bootup will continue.

#what this script will do is create .debug.flg, containing "dropout<n>", that
#the init script will read. The numbers will have to be from 1-6, as 0 is prior
#to the boot- or wkg-partitions being mounted.

export TEXTDOMAIN=debug-initrd
export OUTPUT_CHARSET=UTF-8

. /etc/rc.d/PUPSTATE

if [ ! $DISPLAY ];then #check X still running
 echo "$(gettext 'This script can only be run from the desktop. Quiting.')"
 echo "$(gettext 'Note, you can manually create .debug.flg, at:') /mnt/${BOOT_DEV}/${BOOT_DIR}"
 echo "$(gettext 'with this content:') dropout<n>"
 echo "$(gettext 'where <n> is a number between 1 and 6. Then type \"reboot\"')"
 exit
fi

BOOTflg=0
grep -q "^/dev/${BOOT_DEV} " /proc/mounts
if [ $? -ne 0 ];then
 mkdir -p /mnt/${BOOT_DEV}
 busybox mount -t ${BOOT_FS} /dev/${BOOT_DEV} /mnt/${BOOT_DEV}
 if [ $? -ne 0 ];then
  E1="$(gettext 'ERROR, unable to mount boot-partition:')"
  popup "background=#ff8080 process=wait terminate=ok|${E1} /dev/${BOOT_DEV}"
  exit
 fi
 BOOTflg=1
fi

CR='
'
HELP_HELP_MSG="$(gettext 'If you choose to reboot later, you could then choose a different shutdown method from the Shutdown menu.')${CR}$(gettext 'For example, if later you chose to reboot with copying session to RAM, you could dropout in the initrd, if there is a need to debug that mode of bootup.')${CR}${CR}${CR}<b>$(gettext 'Technical notes')</b>${CR}$(gettext 'EasyDebug creates a file .debug.flg, at this location:')${CR}/mnt/${BOOT_DEV}/${BOOT_DIR}${CR}$(gettext 'With content:') dropoutNUM $(gettext 'Where NUM is a number 1-6')${CR}$(gettext 'Note: some initrd debugging information is in /root/.initrd-debug after bootup.')"
Cm="$(gettext 'Close')"
export DLG_HELP_HELP="<window resizable=\"false\" title=\"Easy Debug: help\" icon-name=\"gtk-index\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${HELP_HELP_MSG}\"</label><variable>VAR_HELP_HELP</variable></text><hbox><button><label>${Cm}</label><action type=\"closewindow\">VAR_HELP_HELP</action></button></hbox></vbox></window>"

export DBG_INITRD_DLG="<window title=\"$(gettext 'Easy Debug:') initrd\" icon-name=\"gtk-about\" window_position=\"1\">
 <vbox>
  <text><label>\"$(gettext 'Reboot computer, dropping out to a shell in the initrd.')${CR}${CR}$(gettext 'At bootup, vmlinuz, the Linux kernel first runs, followed by initrd, which is a complete tiny Linux OS. The initrd creates the layered filesystem and performs a switch_root onto it and hence to the desktop.')${CR}$(gettext 'Developers may choose a place in the init script inside the initrd, to dropout to a shell, and troubleshoot. They may then choose to return to the init script and continue with bootup.')${CR}${CR}$(gettext 'The label on each radiobutton is a description of the dropout-point in the init script. Click one of the radiobuttons and then the Reboot button:')\"</label></text>
  
  <frame>
    <radiobutton><label>$(gettext 'Boot and wkg partitions mounted')</label><variable>BAK_1</variable></radiobutton>
    <radiobutton><label>$(gettext 'Before recovery and maintenance operations')</label><variable>BAK_2</variable></radiobutton>
    <radiobutton><label>$(gettext 'Before version control')</label><variable>BAK_3</variable></radiobutton>
    <radiobutton><label>$(gettext 'Before setting up SFS layers')</label><variable>BAK_4</variable></radiobutton>
    <radiobutton><label>$(gettext 'Before moving mount points to wkg f.s.')</label><variable>BAK_5</variable></radiobutton>
    <radiobutton><label>$(gettext 'Just before switch_root')</label><variable>BAK_6</variable></radiobutton>
    <hbox>
      <button><label>$(gettext 'Reboot later')</label><action>EXIT:rebootlater</action></button>
      <button><label>$(gettext 'Reboot now')</label><action>EXIT:rebootnow</action></button>
      <button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP_HELP</action></button>
    </hbox>
   </frame>

   <hbox>
     <text><label>$(gettext 'If you are not a developer, click the Cancel button to get out of here:')</label></text>
     <button cancel></button>
   </hbox>
 </vbox></window>"
RETVARS="$(gtkdialog --program=DBG_INITRD_DLG --center)"
eval "$RETVARS"

[ -f /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg ] && rm -f /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg
if [ "$EXIT" == "rebootnow" -o "$EXIT" == "rebootlater" ];then
 [ "$BAK_1" == "true" ] && echo -n 'dropout1' > /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg
 [ "$BAK_2" == "true" ] && echo -n 'dropout2' > /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg
 [ "$BAK_3" == "true" ] && echo -n 'dropout3' > /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg
 [ "$BAK_4" == "true" ] && echo -n 'dropout4' > /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg
 [ "$BAK_5" == "true" ] && echo -n 'dropout5' > /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg
 [ "$BAK_6" == "true" ] && echo -n 'dropout6' > /mnt/${BOOT_DEV}/${BOOT_DIR}.debug.flg
 if [ "$EXIT" == "rebootnow" ];then
  if [ $BOOTflg -eq 1 ];then
   sync
   busybox umount /mnt/${BOOT_DEV}
  fi
  exec wmreboot
 fi
fi
 
if [ $BOOTflg -eq 1 ];then
 sync
 busybox umount /mnt/${BOOT_DEV}
fi
 
###end###
