#!/bin/sh
#(c) Copyright Barry Kauler, February 2014, bkhome.org
#Licence: GPL3 (/usr/share/doc/legal)
#/usr/sbin/childproof renames childproofDOT to .childproof then exec.
#140220 first release. Child-proof a Quirky installation.
#140223 change from per-drive to per-partition method.
#141103 restore /usr/sbin/childproofDOT if cancel.
#150228 need LANG=C prefix for printf.

export TEXTDOMAIN=childproof
export OUTPUT_CHARSET=UTF-8

. /etc/rc.d/PUPSTATE #ATADRIVES is all ide/pata/sata drives (not usb, not optical). one line, space-delimited. also want PDEV1

yaf-splash -bg orange -fg black -close never -fontsize large -text "$(gettext 'Please wait...')" &
X1PID=$!

#for internal pata/sata drives...
NOTEBOOK_XML0=''; NOTEBOOK_XML1=''; VBOX0=''; VBOX1=''; ATAPART_XML=''
NUMATA=`echo -n "$ATADRIVES" | wc -w`
if [ $NUMATA -gt 1 ];then
 NOTEBOOK_XML0="<notebook labels=\"$(echo -n "$ATADRIVES" | sed -e 's% $%%' -e 's% %|%g')\">"
 NOTEBOOK_XML1="</notebook>"
 VBOX0="<vbox>"
 VBOX1="</vbox>"
fi
ATA_XML="$NOTEBOOK_XML0"
for ONEATA in $ATADRIVES
do
 DESCR="$(cat /sys/block/${ONEATA}/device/vendor) $(cat /sys/block/${ONEATA}/device/model)"
 DESCR="$(echo -n "$DESCR" | tr -s ' ')"
 DESCR_XML="<text use-markup=\"true\"><label>\"$(gettext 'Drive:') <b>${ONEATA} ${DESCR}</b>\"</label></text>"
 for ONEPART in `cat /proc/partitions | grep "${ONEATA}" | tr -s ' ' | cut -f 5 -d ' ' | tr '\n' ' '`
 do
  [ "$ONEPART" = "$ONEATA" ] && continue
  [ "$ONEPART" = "$PDEV1" ] && continue
  SIZE512=`cat /proc/partitions | grep "${ONEPART}$" | tr -s ' ' | cut -f 4 -d ' '`
  [ $SIZE512 -lt 1000000 ] && continue #ignore. will also ignore extended partition.
  SIZEK=`expr $SIZE512 \/ 2`
  if [ $SIZEK -gt 1048576 ];then #1024*1024
   SIZE="`dc $SIZEK 1048576 \/ p`"
   SIZE="`LANG=C printf "%.1f" $SIZE`G" #150228
  else
   if [ $SIZEK -gt 99 ];then
    SIZE="`expr $SIZEK \/ 1024`M"
   else
    SIZE="`dc $SIZEK 1024 \/ p`"
    SIZE="`LANG=C printf "%.1f" $SIZE`M" #1502028
   fi
  fi
  [ "$SIZE" = "0.0M" ] && SIZE="0"
  FSTYPE="`guess_fstype /dev/$ONEPART 2>/dev/null`"
  [ "$FSTYPE" = "unknown" ] && FSTYPE=$(blkid -c /dev/null /dev/${ONEPART} | grep -w "${ONEPART}" | grep -o ' TYPE=".*"' | cut -f2 -d'"')
  [ "$FSTYPE" = "" ] && FSTYPE="$(gettext 'unknown')"
  [ "$FSTYPE" = "swap" ] && continue #ignore
  ATAPART_XML="${ATAPART_XML}
  <checkbox><label>${ONEPART} $(gettext 'Filesystem:') ${FSTYPE} $(gettext 'Size:') ${SIZE}</label><variable>CHKATA_${ONEPART}</variable></checkbox>"
 done
 ATA_XML="${VBOX0}${DESCR_XML}${ATAPART_XML}${VBOX1}"
done
ATA_XML="${ATA_XML}${NOTEBOOK_XML1}"

APPLY_XML="<button><label>$(gettext 'APPLY')</label><input file>/usr/local/lib/X11/mini-icons/mini-tick.xpm</input><action function=\"exit\">OK</action></button>"
if [ ! "$ATA_XML" ];then
 ATA_XML="<text use-markup=\"true\"><label>\"<b>$(gettext 'There are no fixed internal drives')</b>\"</label></text>"
 APPLY_XML=""
fi

M_w1="<span fgcolor='"'red'"'><b>$(gettext "This operation may only be performed once")</b></span>" #need some funny stuff.

kill $X1PID

WINTITLE="$(gettext 'Childproof Setup')"
export CHILDPROOF_DLG1="<window title=\"${WINTITLE}\" window_position=\"1\" icon-name=\"gtk-preferences\">
<vbox>
 <text use-markup=\"true\"><label>\"${M_w1}\"</label></text>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'Hide partitions from the user!')</b>\"</label></text>
 
 <text><label>$(gettext 'Allow the user full access to the computer, except for selected fixed internal partitions. Tick the partitions that you want to hide:')</label></text>
 ${ATA_XML}
 
 <hbox>
  ${APPLY_XML}
  <button cancel></button>
 </hbox>
</vbox>
</window>"
RETVALS="`gtkdialog --program=CHILDPROOF_DLG1`"
eval "$RETVALS"

if [ "$EXIT" != "OK" ];then
 cp -f /usr/sbin/.childproof /usr/sbin/childproofDOT #141103
 exit
fi

DISABLEPARTS="$(echo "$RETVALS" | grep '^CHKATA_' | grep 'true' | cut -f 1 -d '=' | cut -f 2 -d '_')" #ex: sda5
if [ ! "$DISABLEPARTS" ];then
 pupmessage -bg pink -fg black -title "${WINTITLE}" "$(gettext 'You did not tick any partition checkboxes, so none will be hidden.')"
 cp -f /usr/sbin/.childproof /usr/sbin/childproofDOT #141103
 exit
fi

echo '#!/bin/sh' > /etc/init.d/.childproof
echo '[ $1 ] && [ "$1" != "start" ] && exit 0' >> /etc/init.d/.childproof
for ONEPART in $DISABLEPARTS
do
 echo "rm -f /dev/${ONEPART}" >> /etc/init.d/.childproof
 #don't do it immediately, as may be in use. but, remove the desktop icon handler...
 rm -rf /root/.pup_event/drive_${ONEPART}
done
chmod 755 /etc/init.d/.childproof
echo "ICONWIPE" > /var/local/pup_event_icon_change_flag #/sbin/clean_desk_icons can read this, then wipe all current icons. called from .xinitrc

PARTSLIST="$(echo -n "$DISABLEPARTS" | tr '\n' ' ')"

#no, what happens when install a service pack!...
##booting off flash drive, hide 1st partition also...
BOOT_XML=''
#PPARTNUM="$(echo -n "$PDEV1" | grep -o -E '[a-z][0-9]$|[a-z][0-9][0-9]$' | cut -c 2-)" #ex: 2
#PDRIVE="$(echo -n "$PDEV1" | sed -e "s%${PPARTNUM}$%%" -e 's%p$%%')" #ex: sdb
#P=''
#[ "$(echo -n "$PDEV1" | grep '[0-9]p[0-9]')" != "" ] && P='p' #ex: mmcblk0p1
#if [ "$PPARTNUM" = "2" ];then
# PART1FS="$(guess_fstype /dev/${PDRIVE}${P}1)"
# if [ "$PART1FS" = "vfat" ];then
#  echo 'rm -f `rdev | cut -f 1 -d " " | sed -e "s%[0-9]$%%" -e "s%[0-9]$%%" -e "s%$%1%"`' >> /etc/init.d/.childproof
#  BOOT_XML="<text><label>\" \"</label></text><text><label>$(gettext 'Note: As an extra precaution, the boot partition has also been hidden. This is partition:') ${PDRIVE}${P}1</label></text>"
# fi
#fi

export CHILDPROOF_DLG2="<window title=\"${WINTITLE}\" window_position=\"1\" icon-name=\"gtk-preferences\">
<vbox>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'Childproofing completed!')</b>\"</label></text>
 <text><label>\" \"</label></text>
 <text><label>$(gettext 'These partitions have been hidden:')</label></text>
 <text use-markup=\"true\"><label>\"<b>${PARTSLIST}</b>\"</label></text>
 ${BOOT_XML}
 <text><label>\" \"</label></text>
 <text><label>$(gettext 'A reboot is required to take affect')</label></text>
 <hbox><button ok></button></hbox>
</vbox>
</window>"
RETVALS="`gtkdialog --program=CHILDPROOF_DLG2`"

###END###
