#!/bin/sh
#Barry Kauler Dec. 2010, license GPL v3 /usr/share/doc/legal.
#based on a script by technosaurus, Dec. 2010.
#101222 fix menu.
#101227 technosaurus: fixed reverse dependency check.
#110505 support sudo for non-root user.
#120202 rodin.s: internationalized.
#120329 Xdialog bug --yesno supposed to "yes" "No" buttons, but they are "OK" "Cancel".

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

export TEXTDOMAIN=remove_builtin
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
export LANGORG=$LANG

#export LANG=C #keep Xdialog happy. #off for gettext

Yes_lbl="$(gettext 'Yes')"
No_lbl="$(gettext 'No')"

MSG1="`gettext \"Simple utility to 'delete' packages that are builtin\nto the read-only .sfs file (Squashfs filesystem)\"`"

#!/bin/sh
D=$HOME/.packages/builtin_files

while [ 1 ];do
 FIXMENU='no' #101222
 PKGS=`ls -1 $D`
 PKG=`Xdialog --left --screen-center --backtitle "${MSG1}" --title "$(gettext 'Remove builtin packages')" --stdout --help "$(gettext "In all modes of running Puppy, other than a full hard-drive installation,\n
all of the Puppy files are in a compressed read-only file named 'puppy.sfs'\n
or 'wary_500.sfs' (or some similar name). So you can't actually delete these\n
files. However, if you want to remaster the live-CD to create your own custom\n
Puppy (see Setup menu), you can use this little program to 'pretend' to delete\n
them -- and they will really be gone in the remastered CD. So, for example if\n
you remove SeaMonkey, it will be gone in the remastered CD thus reducing the\n
size of the .iso live-CD file.\n\n
Technical note: the lists of builtin files is at /root/.packages/builtin_files,\n
        the list of builtin pkgs is in /root/.packages/woof-installed-packages.")" --combobox "$(gettext 'Select package to remove:')" 0 0 $PKGS`
 if [ "$PKG" ];then
  aREGEX="|${PKG}|"
  DESCR="`grep "$aREGEX" /root/.packages/woof-installed-packages |cut -d "|" -f 10`"
  Xdialog --left --screen-center --backtitle "$(gettext 'Confirm that you want to delete') '${PKG}'" --title "$(gettext 'Remove builtin packages')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$(gettext 'Description of package:')\n
${DESCR}\n\n
$(gettext 'For information only, these are dependencies of') '${PKG}':\n
`grep "$aREGEX" /root/.packages/woof-installed-packages |cut -d "|" -f 9 | sed -e 's%^+%%' -e 's%,+% %g'`
\n
`eval_gettext \"Warning, removing '\\\${PKG}' _may_ break the following packages:\"`\n
`grep +${PKG} /root/.packages/woof-installed-packages |cut -d "|" -f 2 |tr "\n" " "`
\n
$(gettext 'Continue?')" 0 0
  if [ $? -eq 0 ];then
   [ "`grep '\.desktop$' ${D}/${PKG}`" != "" ] && FIXMENU='yes' #101222
   for x in `cat $D/$PKG`; do [ -d $x ] && cd $x || rm $x; done
   for x in `tac $D/$PKG`; do [ -d $x ] && [ ! "`ls $x`" ] && rmdir $x; done
   rm $D/$PKG
   grep -v "$aREGEX" /root/.packages/woof-installed-packages > /tmp/woof-installed-packages-cut2
   mv -f /tmp/woof-installed-packages-cut2 /root/.packages/woof-installed-packages
   MSG1="`eval_gettext \"Package '\\\${PKG}' was removed. Choose another...\"`"
   if [ "$FIXMENU" = "yes" ];then #101222
    fixmenus
    [ "`pidof jwm`" != "" ] && jwm -restart
    sleep 1
   fi
  else
   MSG1="`eval_gettext \"Package '\\\${PKG}' was not removed. Choose another...\"`"
  fi
  #exec remove_builtin.sh "$MSG1"
  continue
 fi
 break
done
