Advertisement
s243a

remove_builtin.sh (puppy4 - attackpup)

Feb 21st, 2021
1,339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.16 KB | None | 0 0
  1. #!/bin/bash
  2. #Barry Kauler Dec. 2010, license GPL v3 /usr/share/doc/legal.
  3. #based on a script by technosaurus, Dec. 2010.
  4. #Modified by s243a to work with an alternative root and for better cli support.
  5.  
  6. #set -x
  7.  
  8. REMOVE_BUILTIN_fixmenus=no
  9. REMOVE_BUILTIN_prompt=no
  10. CHROOT_UPDATES=no
  11. REMOVE_BUILTIN_use_gui=no
  12. CHROOT_DIR="${CHROOT_DIR:-/mnt/+aufs+devsave+test_save+ext2/Kali/kalipup_CLI.tar.gz.extracted/kalipup/puppy_upupgg+d_20.10.sfs.extracted}"
  13. #echo CHROOT_DIR="CHROOT_DIR=$CHROOT_DIR"
  14. #set +x
  15. #read -p "Press Enter to continue"
  16. #set -x
  17.  
  18. [ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505
  19.  
  20. export TEXTDOMAIN=remove_builtin
  21. export OUTPUT_CHARSET=UTF-8
  22. eval_gettext () {
  23.   local myMESSAGE=$(gettext "$1")
  24.   eval echo \"$myMESSAGE\"
  25. }
  26.  
  27. Yes_lbl="$(gettext 'Yes')"
  28. No_lbl="$(gettext 'No')"
  29.  
  30. MSG1="`gettext \"Simple utility to 'delete' packages that are builtin\nto the read-only .sfs file (Squashfs filesystem)\"`"
  31. FIXMENU='no'
  32.  
  33. D=$CHROOT_DIR/root/.packages/builtin_files
  34. D="$(realpath "$D")"
  35. [ ! -d "$D" ] && D=$CHROOT_DIR/var/packages/builtin_files
  36.  
  37. #===========================================================
  38. process_remove_builtin_list() {
  39.     # count
  40.     #set -x
  41.     local pkglist="$1" COUNT=$(wc -l < "$1") COUNTER=0
  42.     mkdir -p /tmp/deb-build/
  43.     #exec 15<> /tmp/deb-build/fd_15
  44.    
  45.    
  46.     # process
  47.     while read -r p; do
  48.         echo "Processing: $p"
  49.         p=${p%%#*}; eval set -- $p; p="$1"
  50.         [ -z $p ] && continue
  51.        
  52.         # commands
  53.         COUNTER=$((COUNTER+1))     
  54.         echo $COUNTER of $COUNT "$@"       
  55.         case $p in
  56.             #%exit) break ;;
  57.             #%dpkg)
  58.             #   echo Switching to dpkg
  59.             #   [ -z "$WITHOUT_DPKG" ] && do_install() { dpkg_install; } ||
  60.             #   do_install() { dpkgchroot_install; } ;;
  61.             #%dpkgchroot)
  62.             #   echo Switching to dpkgchroot
  63.             #   do_install() { dpkgchroot_install; } ;;
  64.             #%bootstrap)
  65.             #   echo Switching to bootstrap
  66.             #   do_install() { bootstrap_install; } ;;
  67.             #%bblinks)
  68.             #   shift # $1-nousr
  69.             #   echo Installing busybox symlinks ...
  70.             #   install_bb_links "$@" ;;
  71.             #%makesfs)
  72.             #    set -x
  73.             #    #unbind_ALL
  74.             #    wait_until_unmounted || exit
  75.             #   shift # $1-output $@-squashfs params
  76.             #   echo Creating $1 ...
  77.             #   make_sfs "$@" ;;
  78.             #%remove)
  79.             #   shift # $1-pkgname, pkgname, ...
  80.             #   remove_pkg "$@" ;;
  81.             #%addbase)
  82.             #   echo Installing base rootfs ...
  83.             #   [ "$BASE_ARCH_PATH" ] && install_from_dir $BASE_ARCH_PATH base-arch core               
  84.             #   install_from_dir $BASE_CODE_PATH base core ;;
  85.             #%addpkg)
  86.             #    set -x
  87.             #   shift # $1-pkgname, pkgname ...
  88.             #   forced=0
  89.             #   PKGSECTION="optional"
  90.             #   while [ "$1" ]; do
  91.             #       case "$1" in
  92.             #       --forced)
  93.             #         forced=1
  94.             #         ;;
  95.             #       --category=*)
  96.             #         PKGSECTION="${1#'--category='}";
  97.             #         ;;
  98.             #       *)
  99.             #         ! [ -d $EXTRAPKG_PATH/$1 ] && shift && continue
  100.             #         echo Installing extra package "$1"  ...
  101.             #         if [ $forced -eq 1 ]; then
  102.             #           install_from_dir $EXTRAPKG_PATH/$1 $1 $PKGSECTION force
  103.             #         else
  104.             #           install_from_dir $EXTRAPKG_PATH/$1 $1 $PKGSECTION
  105.             #         fi
  106.             #         ;;
  107.             #       esac
  108.             #       shift
  109.             #   done
  110.             #   set +x
  111.             #    ;;
  112.             #%importpkg)
  113.             #  shift
  114.             #  set -x
  115.             #  import_from_dir $EXTRAPKG_PATH/$1
  116.             #  set +x
  117.             #    ;;
  118.             #%dummy)
  119.             #    set -x
  120.             #   shift # $1-pkgname, pkgname ...
  121.             #   install_dummy "$@"
  122.             #   set +x
  123.             #   ;;
  124.             #%dpkg_configure)
  125.             #   shift # $@-configure flags
  126.             #   DEBIAN_FRONTEND=noninteractive chroot $CHROOT_DIR /usr/bin/dpkg --configure "$@" ;;
  127.             #%lock)
  128.             #   shift # $1-pkgname, pkgname ...
  129.             #   lock_pkg "$@" ;;
  130.             #%cutdown)
  131.             #    set -x
  132.             #   shift # $@ various cutdown options
  133.             #   cutdown "$@" ;;
  134.             #%import)
  135.             #   shift # $@ dirs to import
  136.             #   import_dir "$@" ;;
  137.             #  
  138.             #### filesystem operations
  139.             #%symlink)
  140.             #   shift # $1 source $2 target
  141.             #   rm -f $CHROOT_DIR/$2
  142.             #   ln -s $1 $CHROOT_DIR/$2
  143.             #   ;;
  144.             #%rm)
  145.             #   shift # $@ files to remove
  146.             #   while [ "$1" ]; do
  147.             #       rm -rf $CHROOT_DIR/$1
  148.             #       shift
  149.             #   done
  150.             #   ;;
  151.             #%mkdir)
  152.             #   shift # $@ dirs to make
  153.             #   while [ "$1" ]; do
  154.             #       mkdir -p $CHROOT_DIR/$1
  155.             #       shift
  156.             #   done
  157.             #   ;;
  158.             #%touch)
  159.             #   shift # $@ files to create
  160.             #   while [ "$1" ]; do
  161.             #       > $CHROOT_DIR/$1
  162.             #       shift
  163.             #   done
  164.             #   ;;
  165.             #%chroot)
  166.             #   shift # $@ commands
  167.             #   chroot $CHROOT_DIR "$@"
  168.             #   ;;
  169.             #
  170.             # anything else - install package
  171.             *)
  172.                 #get_pkg_info $p
  173.                 #[ -z "$PKG" ] && echo Cannot find ${p}. && continue
  174.                 #download_pkg || { echo Download $p failed. && exit 1; }
  175.                 #install_pkg "$2" || { echo Installation of $p failed. && exit 1; }
  176.                 remove_builtin_pkg "$p"        
  177.                 ;;
  178.         esac       
  179.     done < "$pkglist" #< $pkglist
  180.     #exec 15>&-
  181.     #rm /tmp/deb-build/fd_14
  182.     return 0
  183. }
  184.  
  185. function remove_builtin_pkg() {
  186.     PKG="$1"
  187.     local NO_PKG=false
  188.     if [ -z "$PKG" ] ; then
  189.         return 1
  190.     fi
  191.     if [ ! -f $D/$PKG ] ; then
  192.         echo "$PKG does not exist.." >&2
  193.         #return 1 #TODO: we need alternative to "return 1" here
  194.         NO_PKG=true
  195.     else
  196.       echo "Removing $PKG"
  197.       if grep -q '\.desktop$' ${D}/${PKG}; then
  198.         [ "$REMOVE_BUILTIN_fixmenus" = yes ] && FIXMENU='yes' #101222
  199.       fi
  200.       (
  201.         while read file
  202.         do
  203.             rm "$CHROOT_DIR$file" 1>&2
  204.             echo "${file%/*}" >&2 #get dir
  205.         done < $D/$PKG
  206.       ) > /tmp/remove_builtin_dirs$$
  207.       #-- remove empty directories
  208.       sort -ur /tmp/remove_builtin_dirs$$ | \
  209.       while read dir
  210.       do
  211.         while [ 1 ] ; do
  212.             rmdir "$dir" 2>/dev/null
  213.             dir=${dir%/*} #dirname $dir
  214.             if [ -z "$dir" ] ; then
  215.                 break
  216.             fi
  217.         done
  218.       done
  219.     #--
  220.   fi
  221.   PKGFILES="$D/$PKG"
  222.  
  223.   if [ "$CHROOT_UPDATES" = yes ] && [ "$NO_PKG" = false ]; then
  224.     if [ "`grep '/usr/share/glib-2.0/schemas' $PKGFILES`" != "" ];then
  225.      [ -e /usr/bin/glib-compile-schemas ] && glib-compile-schemas /usr/share/glib-2.0/schemas
  226.     fi
  227.    
  228.     if [ "`grep '/usr/lib/gio/modules' $PKGFILES`" != "" ];then
  229.      [ -e /usr/bin/gio-querymodules ] && gio-querymodules /usr/lib/gio/modules
  230.     fi
  231.    
  232.     if [ "`grep ''\.desktop$'' $PKGFILES`" != "" ];then
  233.      rm -f "$CHROOT_DIR/usr/share/applications/mimeinfo.cache"
  234.      [ -e /usr/bin/update-desktop-database ] && update-desktop-database /usr/share/applications
  235.     fi
  236.    
  237.     if [ "`grep '/usr/share/mime' $PKGFILES`" != "" ];then
  238.      [ -e /usr/bin/update-mime-database ] && update-mime-database /usr/share/mime
  239.     fi
  240.    
  241.     if [ "`grep '/usr/share/icons/hicolor' $PKGFILES`" != "" ];then
  242.      [ -e /usr/bin/gtk-update-icon-cache ] && gtk-update-icon-cache /usr/share/icons/hicolor
  243.     fi
  244.    
  245.     if [ "`grep '/usr/lib/gdk-pixbuf' $PKGFILES`" != "" ];then
  246.      gdk-pixbuf-query-loaders --update-cache
  247.     fi
  248.    
  249.     if [ "`grep '/usr/lib/gconv' $PKGFILES`" != "" ];then
  250.      iconvconfig
  251.     fi
  252.    
  253.     if [ "`grep '/usr/lib/pango' $PKGFILES`" != "" ];then
  254.      pango-querymodules --update-cache
  255.     fi
  256.  
  257.  
  258.     for gtkver in '1.0' '2.0' '3.0'
  259.     do
  260.      if [ "`grep "/usr/lib/gtk-$gtkver" $PKGFILES | grep "/immodules"`" != "" ];then
  261.       [ -e /usr/bin/gtk-query-immodules-$gtkver ] && gtk-query-immodules-$gtkver --update-cache
  262.      fi
  263.     done
  264.    
  265.     if [ "`grep '/usr/share/fonts' $PKGFILES`" != "" ];then
  266.      fc-cache -f
  267.     fi
  268.    
  269.     KERNVER="$(uname -r)"
  270.    
  271.     if [ "`grep "/lib/modules/$KERNVER" $PKGFILES`" != "" ];then
  272.      depmod -a
  273.     fi
  274.   fi   
  275.    
  276.   [ -f $D/$PKG ] && rm $D/$PKG
  277.   [ "$NO_PKG" = false ] && rm /tmp/remove_builtin_dirs$$
  278.   sed -i "\%|${PKG}|%d" "$CHROOT_DIR/root/.packages/woof-installed-packages"
  279.   line="$(cat "$CHROOT_DIR/root/.packages/DISTRO_PKGS_SPECS" | grep "^yes|${PKG}|")"
  280.   pkgs_str=$(echo $line | cut -d'|' -f3)
  281.   pkgs=(${pkgs_str//,/" "})
  282.   for a_pkg in "${pkgs[@]}"; do
  283.     sed -i "\%|${a_pkg}|%d" "$CHROOT_DIR/root/.packages/woof-installed-packages"
  284.   done
  285.   return 0
  286. }
  287.  
  288. fix_menus() {
  289.     if [ "$FIXMENU" = "yes" ];then #101222
  290.         fixmenus
  291.         if [ "$DISPLAY" ] ; then
  292.             if [ "`pidof jwm`" != "" ] ; then
  293.                 jwm -reload || jwm -restart
  294.                 sleep 1
  295.             fi
  296.         fi
  297.     fi
  298. }
  299.  
  300. #===========================================================
  301. #                   COMMAND LINE
  302. #============================================================
  303.  
  304. case $1 in
  305.   -l|-list|--list)
  306.     ls -1 $D
  307.     exit ;;
  308.   --pkg-list)
  309.     shift
  310.     process_remove_builtin_list "$@"
  311.     exit ;;    
  312. esac
  313.  
  314. if [ "$1" ] ; then
  315.     for i in $@
  316.     do
  317.         remove_builtin_pkg $i
  318.     done
  319.     [ "$REMOVE_BUILTIN_fixmenus" = yes ] && fix_menus
  320.     exit $?
  321. fi
  322.  
  323. #===========================================================
  324. #                    GUI
  325. #============================================================
  326.  
  327.  
  328. if [ REMOVE_BUILTIN_use_gui = yes ]; then
  329.     PKGS=`ls -1 $D`
  330.     DIALOG="dialog --aspect 10"
  331.     MENUOPT="--checklist"
  332.     REP=/tmp/$(basename $0).txt
  333.    
  334.     if [ "$DISPLAY" ] ; then
  335.         DIALOG=Xdialog
  336.     fi
  337.    
  338.     for i in $PKGS ; do
  339.         CHOICES="$CHOICES $i . off"
  340.     done
  341.    
  342.     PKG=`$DIALOG --stdout --backtitle "${MSG1}" --title "$(gettext 'Remove builtin packages')" --help "$(gettext "In all modes of running Puppy, other than a full hard-drive installation,\n
  343.     all of the Puppy files are in a compressed read-only file named 'puppy.sfs'\n
  344.     or 'wary_500.sfs' (or some similar name). So you can't actually delete these\n
  345.     files. However, if you want to remaster the live-CD to create your own custom\n
  346.     Puppy (see Setup menu), you can use this little program to 'pretend' to delete\n
  347.     them -- and they will really be gone in the remastered CD. So, for example if\n
  348.     you remove SeaMonkey, it will be gone in the remastered CD thus reducing the\n
  349.     size of the .iso live-CD file.\n\n
  350.     Technical note: the lists of builtin files is at /root/.packages/builtin_files,\n
  351.     the list of builtin pkgs is in /root/.packages/woof-installed-packages.")" \
  352.     $MENUOPT "$(gettext 'Select packages to remove (be careful):')" 0 0 0 $CHOICES`
  353.     if [ ! "$PKG" ];then
  354.         exit
  355.     fi
  356.    
  357.     PKG=$(echo "$PKG" | sed 's%/% %g')
  358.     /usr/lib/gtkdialog/box_yesno --warning "Remove builtin pkg(s)" \
  359.         "Please confirm that you want to delete the following pkg(s):" "" \
  360.         "<b>$PKG</b>" "" "This can be a dangerous operation if you don't know what you're doing. However some more confirmation dialogs may appear..."
  361.     if [ $? -ne 0 ] ; then
  362.         exit
  363.     fi
  364.    
  365.     #--
  366.     REMOVED_PKGS=''
  367.     #--
  368.    
  369.     for i in $PKG
  370.     do
  371.         DEP_OF=`grep "+${i}" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 2 |tr "\n" " "`
  372.         if [ "$DEP_OF" ] ; then
  373.             DESCR="`grep "|${i}|" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 10`"
  374.             Xdialog --left --screen-center --backtitle "$(gettext 'Confirm that you want to delete') '${i}'" --title "$(gettext 'Remove builtin packages')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$(gettext 'Description of package:')\n
  375.     ${DESCR}\n
  376.     $(gettext 'For information only, these are dependencies of') '${i}':\n
  377.     `grep "|${i}|" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 9 | sed -e 's%^+%%' -e 's%,+% %g'`
  378.     \n
  379.     `eval_gettext \"Warning, removing '\\\${i}' _may_ break the following packages:\"`
  380.     $DEP_OF
  381.     \n
  382.     $(gettext 'Continue?')" 0 0
  383.             if ! [ $? -eq 0 ];then
  384.                 continue
  385.             fi
  386.         fi
  387.         remove_builtin_pkg $i || continue
  388.         REMOVED_PKGS="$REMOVED_PKGS $i"
  389.     done
  390. fi
  391.  
  392. [ "$REMOVE_BUILTIN_fixmenus" = yes ] && fix_menus
  393.  
  394. if [ "$REMOVE_BUILTIN_use_gui" = yes ]; then
  395.     if [ "${REMOVED_PKGS}" ] ; then
  396.         /usr/lib/gtkdialog/box_ok "Remove builtin pkg(s)" info  \
  397.             "The following packages were removed:" "" "<b>${REMOVED_PKGS}</b>"
  398.     fi
  399. fi
  400. ### END ###
  401.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement