#!/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".
#170303 modified for Easy Linux.
#180523 removing seems not working properly. be a bit more verbose.
#181122 q*.sfs renamed to easy*.sfs

[ "`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

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

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

D=$HOME/.packages/builtin_files

while [ 1 ];do
 FIXMENU='no' #101222
 PKGS=`ls -1 $D`
 PKG=`Xdialog --wrap --left --screen-center --backtitle "${MSG1}" --title "$(gettext 'Remove builtin packages')" --stdout --help "$(gettext "Easy Linux runs on a read-only filesystem, a file named 'easy.sfs'. So you can't actually delete these files. However, if you want to remaster Easy Linux, that is, create your own customized version, you can 'pretend' to delete them here, and they will be gone in the remastered easy.sfs. So, for example if you remove SeaMonkey, it will be gone in the remastered easy.sfs thus reducing the size.\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 80 $PKGS`
 if [ "$PKG" ];then
  aREGEX="|${PKG}|"
  DESCR="`grep "$aREGEX" /root/.packages/woof-installed-packages |cut -d "|" -f 10`"
  Xdialog --wrap --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 120
  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
   #180523 above seems not working properly. be a bit more verbose...
   aDIR=''; aOLD=''
   echo "Removing ${PKG}:" > /tmp/remove_builtin.log
   while read aLINE
   do
    CH1="${aLINE:0:1}"
    if [ "$CH1" == "/" ];then
     aOLD="$aDIR"
     if [ "$aOLD" ];then
      [ ! -d "$aOLD" ] && continue #precaution
      rmdir "$aOLD" 2>/dev/null
      [ $? -eq 0 ] && echo "DIR: ${aOLD}" >> /tmp/remove_builtin.log
     fi
     aDIR="$aLINE"
    else
     [ ! "$aDIR" ] && continue #precaution
     #aFILE="${aLINE:1:199}"
     #...oh, the leading space char is missing. odd. well, check anyway...
     aFILE="$(echo -n "$aLINE" | sed -e 's%^ %%')"
     [ ! -e "${aDIR}/${aFILE}" ] && continue #precaution
     echo "FILE: ${aDIR}/${aFILE}" >> /tmp/remove_builtin.log
     rm -f "${aDIR}/${aFILE}"
    fi
   done<<_END1
$(cat ${D}/${PKG})
_END1
   
   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
   sync #180523
   MSG1="`eval_gettext \"Package '\\\${PKG}' was removed. Choose another...\"`"
   if [ "$FIXMENU" = "yes" ];then #101222
    fixmenus
    [ "`pidof jwm`" != "" ] && jwm -reload
    sleep 1
   fi
  else
   MSG1="`eval_gettext \"Package '\\\${PKG}' was not removed. Choose another...\"`"
  fi
  #exec remove_builtin.sh "$MSG1"
  continue
 fi
 break
done
