Advertisement
s243a

move_builtin.sh (Draft #1)

Mar 17th, 2021 (edited)
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.65 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. CWD="$PWD"
  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:-"$(echo "$CWD/puppy_fossapup64_9.5.sfs.extracted")"}"
  13. TARGET_DIR="${TARGET_DIR:-"$(echo "$CWD/fossapup64_9.5_basesfs_corecli")"}"
  14. #D=$CHROOT_DIR/root/.packages/builtin_files
  15. D_rel=/var/packages/builtin_files-sml
  16. D2_rel=/var/packages/builtin_files-sml_corecli
  17.  
  18. [ -z "$D_rel" ] && D_rel=/root/.packages/builtin_files
  19. D="$CHROOT_DIR$D_rel"
  20. D="$(realpath "$D")"
  21. [ ! -d "$D" ] && D=$CHROOT_DIR/var/packages/builtin_files
  22. D2="$TARGET_DIR$D2_rel"
  23. mkdir -p "$D2"
  24. D2="$(realpath "$D2")"
  25. [ ! -d "$D" ] && D2=$TARGET_DIR/var/packages/builtin_files
  26. #/var/packages/builtin_files-sml
  27. set +x
  28. read -p "press enter to continue"
  29. set -x
  30. mkdir -p "$TARGET_DIR"
  31. function mk_parrent_dir(){
  32.   parrent="$(dirname $1)"
  33.   if  [ ${#parrent} -le 1 ]; then
  34.     return 0
  35.   else [ ! -d "$TARGET_DIR$parrent" ];
  36.     if [ ! -z "`which cpio`" ]; then
  37.         ( cd "$CHROOT_DIR"
  38.           echo "./$1" | \
  39.             cpio -pdu "$TARGET_DIR" )
  40.     else
  41.       mkdir -p "$TARGET_DIR/$1"
  42.     fi         
  43.   fi
  44. }
  45. if [ -z "$BUILTIN_ACITON" ]; then
  46.      if [ -z "$TARGET_DIR" ]; then
  47.       ACTION_MESSAGE="removing"
  48.     else
  49.       ACTION_MESSAGE="moving"
  50.     fi
  51. else
  52.   case "$BUILTIN_ACITON" in
  53.   rm) ACTION_MESSAGE="removing"; ;;
  54.   mv) ACTION_MESSAGE="moving"; ;;
  55.   cp) ACTION_MESSAGE="copying"; ;;
  56.   esac
  57. fi
  58. function builtin_action(){
  59.   local a_action
  60.   if [ -z "$BUILTIN_ACITON" ]; then
  61.     if [ -z "$TARGET_DIR" ]; then
  62.       a_action="rm"
  63.     else
  64.       a_action="mv"
  65.     fi
  66.   else
  67.     a_action="$BUILTIN_ACITON"
  68.   fi
  69.       case "$a_action" in
  70.       rm)
  71.         rm $CHROOT_DIR$1
  72.         ;;
  73.       mv)
  74.         mk_parrent_dir "$1"
  75.         mv "$CHROOT_DIR$1" "$TARGET_DIR$1"
  76.         ;;
  77.       cp) #TODO: use cpio instead of cp.
  78.         if [ ! -z "`which cpio`" ]; then
  79.          ( cd "$CHROOT_DIR"
  80.               echo "./$1" | \
  81.                 cpio -pdu "$TARGET_DIR" )    
  82.         else
  83.           cp -a "$CHROOT_DIR$1" "$TARGET_DIR$1"
  84.         fi
  85.         ;;
  86.       esac
  87.  
  88. }
  89. #[ -f $D/$PKG ] && rm $D/$PKG
  90. function d_action(){
  91.   local a_action
  92.   if [ -z "$BUILTIN_ACITON" ]; then
  93.     if [ -z "$TARGET_DIR" ]; then
  94.       a_action="rm"
  95.     else
  96.       a_action="mv"
  97.     fi
  98.   else
  99.     a_action="$BUILTIN_ACITON"
  100.   fi
  101.       case "$a_action" in
  102.       rm)
  103.         rm $D/$1
  104.         ;;
  105.       mv)
  106.         #mk_parrent_dir "$1"
  107.         mv "$D/$1" "$D2/$1"
  108.         ;;
  109.       cp) #TODO: use cpio instead of cp.
  110.         if [ ! -z "`which cpio`" ]; then
  111.          ( cd "$CHROOT_DIR$D"
  112.               echo "./$1" | \
  113.                 cpio -pdu "$TARGET_DIR$D2" )    
  114.         else
  115.           cp -a "$D/$1" "$D2/$1"
  116.         fi
  117.         ;;
  118.       esac
  119.    
  120. }
  121. #echo CHROOT_DIR="CHROOT_DIR=$CHROOT_DIR"
  122. #set +x
  123. #read -p "Press Enter to continue"
  124. #set -x
  125.  
  126. [ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505
  127.  
  128. export TEXTDOMAIN=remove_builtin
  129. export OUTPUT_CHARSET=UTF-8
  130. eval_gettext () {
  131.   local myMESSAGE=$(gettext "$1")
  132.   eval echo \"$myMESSAGE\"
  133. }
  134.  
  135. Yes_lbl="$(gettext 'Yes')"
  136. No_lbl="$(gettext 'No')"
  137.  
  138. MSG1="`gettext \"Simple utility to 'delete' packages that are builtin\nto the read-only .sfs file (Squashfs filesystem)\"`"
  139. FIXMENU='no'
  140.  
  141. #D=$CHROOT_DIR/root/.packages/builtin_files
  142. #D="$(realpath "$D")"
  143. #[ ! -d "$D" ] && D=$CHROOT_DIR/var/packages/builtin_files
  144.  
  145. #===========================================================
  146. process_remove_builtin_list() {
  147.     # count
  148.     #set -x
  149.     local pkglist="$1" COUNT=$(wc -l < "$1") COUNTER=0
  150.     mkdir -p /tmp/deb-build/
  151.     #exec 15<> /tmp/deb-build/fd_15
  152.    
  153.    
  154.     # process
  155.     while read -r p; do
  156.         echo "Processing: $p"
  157.         p=${p%%#*}; eval set -- $p; p="$1"
  158.         [ -z $p ] && continue
  159.        
  160.         # commands
  161.         COUNTER=$((COUNTER+1))     
  162.         echo $COUNTER of $COUNT "$@"       
  163.         case $p in
  164.             #%exit) break ;;
  165.             #%dpkg)
  166.             #   echo Switching to dpkg
  167.             #   [ -z "$WITHOUT_DPKG" ] && do_install() { dpkg_install; } ||
  168.             #   do_install() { dpkgchroot_install; } ;;
  169.             #%dpkgchroot)
  170.             #   echo Switching to dpkgchroot
  171.             #   do_install() { dpkgchroot_install; } ;;
  172.             #%bootstrap)
  173.             #   echo Switching to bootstrap
  174.             #   do_install() { bootstrap_install; } ;;
  175.             #%bblinks)
  176.             #   shift # $1-nousr
  177.             #   echo Installing busybox symlinks ...
  178.             #   install_bb_links "$@" ;;
  179.             #%makesfs)
  180.             #    set -x
  181.             #    #unbind_ALL
  182.             #    wait_until_unmounted || exit
  183.             #   shift # $1-output $@-squashfs params
  184.             #   echo Creating $1 ...
  185.             #   make_sfs "$@" ;;
  186.             #%remove)
  187.             #   shift # $1-pkgname, pkgname, ...
  188.             #   remove_pkg "$@" ;;
  189.             #%addbase)
  190.             #   echo Installing base rootfs ...
  191.             #   [ "$BASE_ARCH_PATH" ] && install_from_dir $BASE_ARCH_PATH base-arch core               
  192.             #   install_from_dir $BASE_CODE_PATH base core ;;
  193.             #%addpkg)
  194.             #    set -x
  195.             #   shift # $1-pkgname, pkgname ...
  196.             #   forced=0
  197.             #   PKGSECTION="optional"
  198.             #   while [ "$1" ]; do
  199.             #       case "$1" in
  200.             #       --forced)
  201.             #         forced=1
  202.             #         ;;
  203.             #       --category=*)
  204.             #         PKGSECTION="${1#'--category='}";
  205.             #         ;;
  206.             #       *)
  207.             #         ! [ -d $EXTRAPKG_PATH/$1 ] && shift && continue
  208.             #         echo Installing extra package "$1"  ...
  209.             #         if [ $forced -eq 1 ]; then
  210.             #           install_from_dir $EXTRAPKG_PATH/$1 $1 $PKGSECTION force
  211.             #         else
  212.             #           install_from_dir $EXTRAPKG_PATH/$1 $1 $PKGSECTION
  213.             #         fi
  214.             #         ;;
  215.             #       esac
  216.             #       shift
  217.             #   done
  218.             #   set +x
  219.             #    ;;
  220.             #%importpkg)
  221.             #  shift
  222.             #  set -x
  223.             #  import_from_dir $EXTRAPKG_PATH/$1
  224.             #  set +x
  225.             #    ;;
  226.             #%dummy)
  227.             #    set -x
  228.             #   shift # $1-pkgname, pkgname ...
  229.             #   install_dummy "$@"
  230.             #   set +x
  231.             #   ;;
  232.             #%dpkg_configure)
  233.             #   shift # $@-configure flags
  234.             #   DEBIAN_FRONTEND=noninteractive chroot $CHROOT_DIR /usr/bin/dpkg --configure "$@" ;;
  235.             #%lock)
  236.             #   shift # $1-pkgname, pkgname ...
  237.             #   lock_pkg "$@" ;;
  238.             #%cutdown)
  239.             #    set -x
  240.             #   shift # $@ various cutdown options
  241.             #   cutdown "$@" ;;
  242.             #%import)
  243.             #   shift # $@ dirs to import
  244.             #   import_dir "$@" ;;
  245.             #  
  246.             #### filesystem operations
  247.             #%symlink)
  248.             #   shift # $1 source $2 target
  249.             #   rm -f $CHROOT_DIR/$2
  250.             #   ln -s $1 $CHROOT_DIR/$2
  251.             #   ;;
  252.             #%rm)
  253.             #   shift # $@ files to remove
  254.             #   while [ "$1" ]; do
  255.             #       rm -rf $CHROOT_DIR/$1
  256.             #       shift
  257.             #   done
  258.             #   ;;
  259.             #%mkdir)
  260.             #   shift # $@ dirs to make
  261.             #   while [ "$1" ]; do
  262.             #       mkdir -p $CHROOT_DIR/$1
  263.             #       shift
  264.             #   done
  265.             #   ;;
  266.             #%touch)
  267.             #   shift # $@ files to create
  268.             #   while [ "$1" ]; do
  269.             #       > $CHROOT_DIR/$1
  270.             #       shift
  271.             #   done
  272.             #   ;;
  273.             #%chroot)
  274.             #   shift # $@ commands
  275.             #   chroot $CHROOT_DIR "$@"
  276.             #   ;;
  277.             #
  278.             # anything else - install package
  279.             *)
  280.                 #get_pkg_info $p
  281.                 #[ -z "$PKG" ] && echo Cannot find ${p}. && continue
  282.                 #download_pkg || { echo Download $p failed. && exit 1; }
  283.                 #install_pkg "$2" || { echo Installation of $p failed. && exit 1; }
  284.                 remove_builtin_pkg "$p"        
  285.                 ;;
  286.         esac       
  287.     done < "$pkglist" #< $pkglist
  288.     #exec 15>&-
  289.     #rm /tmp/deb-build/fd_14
  290.     return 0
  291. }
  292.  
  293. function remove_builtin_pkg() {
  294.     PKG="$1"
  295.     local NO_PKG=false
  296.     if [ -z "$PKG" ] ; then
  297.         return 1
  298.     fi
  299.     if [ ! -f $D/$PKG ] ; then
  300.         echo "$PKG does not exist.." >&2
  301.         #return 1 #TODO: we need alternative to "return 1" here
  302.         NO_PKG=true
  303.     else
  304.       echo "$ACTION_MESSAGE $PKG"
  305.       if grep -q '\.desktop$' ${D}/${PKG}; then
  306.         [ "$REMOVE_BUILTIN_fixmenus" = yes ] && FIXMENU='yes' #101222
  307.       fi
  308.       (
  309.         while read file
  310.         do
  311.             #rm "$CHROOT_DIR$file" 1>&2
  312.             builtin_action "$file" 1>&2
  313.             echo "${file%/*}" >&2 #get dir
  314.         done < $D/$PKG
  315.       ) > /tmp/remove_builtin_dirs$$
  316.       #-- remove empty directories
  317.       sort -ur /tmp/remove_builtin_dirs$$ | \
  318.       while read dir
  319.       do
  320.         while [ 1 ] ; do
  321.             rmdir "$dir" 2>/dev/null
  322.             dir=${dir%/*} #dirname $dir
  323.             if [ -z "$dir" ] ; then
  324.                 break
  325.             fi
  326.         done
  327.       done
  328.     #--
  329.   fi
  330.   PKGFILES="$D/$PKG"
  331.  
  332.   if [ "$CHROOT_UPDATES" = yes ] && [ "$NO_PKG" = false ]; then
  333.     if [ "`grep '/usr/share/glib-2.0/schemas' $PKGFILES`" != "" ];then
  334.      [ -e /usr/bin/glib-compile-schemas ] && glib-compile-schemas /usr/share/glib-2.0/schemas
  335.     fi
  336.    
  337.     if [ "`grep '/usr/lib/gio/modules' $PKGFILES`" != "" ];then
  338.      [ -e /usr/bin/gio-querymodules ] && gio-querymodules /usr/lib/gio/modules
  339.     fi
  340.    
  341.     if [ "`grep ''\.desktop$'' $PKGFILES`" != "" ];then
  342.      rm -f "$CHROOT_DIR/usr/share/applications/mimeinfo.cache"
  343.      [ -e /usr/bin/update-desktop-database ] && update-desktop-database /usr/share/applications
  344.     fi
  345.    
  346.     if [ "`grep '/usr/share/mime' $PKGFILES`" != "" ];then
  347.      [ -e /usr/bin/update-mime-database ] && update-mime-database /usr/share/mime
  348.     fi
  349.    
  350.     if [ "`grep '/usr/share/icons/hicolor' $PKGFILES`" != "" ];then
  351.      [ -e /usr/bin/gtk-update-icon-cache ] && gtk-update-icon-cache /usr/share/icons/hicolor
  352.     fi
  353.    
  354.     if [ "`grep '/usr/lib/gdk-pixbuf' $PKGFILES`" != "" ];then
  355.      gdk-pixbuf-query-loaders --update-cache
  356.     fi
  357.    
  358.     if [ "`grep '/usr/lib/gconv' $PKGFILES`" != "" ];then
  359.      iconvconfig
  360.     fi
  361.    
  362.     if [ "`grep '/usr/lib/pango' $PKGFILES`" != "" ];then
  363.      pango-querymodules --update-cache
  364.     fi
  365.  
  366.  
  367.     for gtkver in '1.0' '2.0' '3.0'
  368.     do
  369.      if [ "`grep "/usr/lib/gtk-$gtkver" $PKGFILES | grep "/immodules"`" != "" ];then
  370.       [ -e /usr/bin/gtk-query-immodules-$gtkver ] && gtk-query-immodules-$gtkver --update-cache
  371.      fi
  372.     done
  373.    
  374.     if [ "`grep '/usr/share/fonts' $PKGFILES`" != "" ];then
  375.      fc-cache -f
  376.     fi
  377.    
  378.     KERNVER="$(uname -r)"
  379.    
  380.     if [ "`grep "/lib/modules/$KERNVER" $PKGFILES`" != "" ];then
  381.      depmod -a
  382.     fi
  383.   fi   
  384.    
  385.   #[ -f $D/$PKG ] && rm $D/$PKG
  386.   d_action "$PKG"
  387.   [ "$NO_PKG" = false ] && rm /tmp/remove_builtin_dirs$$
  388.   sed -i "\%|${PKG}|%d" "$CHROOT_DIR/root/.packages/woof-installed-packages"
  389.   line="$(cat "$CHROOT_DIR/root/.packages/DISTRO_PKGS_SPECS" | grep "^yes|${PKG}|")"
  390.   pkgs_str=$(echo $line | cut -d'|' -f3)
  391.   pkgs=(${pkgs_str//,/" "})
  392.   for a_pkg in "${pkgs[@]}"; do
  393.     sed -i "\%|${a_pkg}|%d" "$CHROOT_DIR/root/.packages/woof-installed-packages"
  394.   done
  395.   return 0
  396. }
  397.  
  398. fix_menus() {
  399.     if [ "$FIXMENU" = "yes" ];then #101222
  400.         fixmenus
  401.         if [ "$DISPLAY" ] ; then
  402.             if [ "`pidof jwm`" != "" ] ; then
  403.                 jwm -reload || jwm -restart
  404.                 sleep 1
  405.             fi
  406.         fi
  407.     fi
  408. }
  409.  
  410. #===========================================================
  411. #                   COMMAND LINE
  412. #============================================================
  413.  
  414. case $1 in
  415.   -l|-list|--list)
  416.     ls -1 $D
  417.     exit ;;
  418.   --pkg-list)
  419.     shift
  420.     process_remove_builtin_list "$@"
  421.     exit ;;    
  422. esac
  423.  
  424. if [ "$1" ] ; then
  425.     for i in $@
  426.     do
  427.         remove_builtin_pkg $i
  428.     done
  429.     [ "$REMOVE_BUILTIN_fixmenus" = yes ] && fix_menus
  430.     exit $?
  431. fi
  432.  
  433. #===========================================================
  434. #                    GUI
  435. #============================================================
  436.  
  437.  
  438. if [ REMOVE_BUILTIN_use_gui = yes ]; then
  439.     PKGS=`ls -1 $D`
  440.     DIALOG="dialog --aspect 10"
  441.     MENUOPT="--checklist"
  442.     REP=/tmp/$(basename $0).txt
  443.    
  444.     if [ "$DISPLAY" ] ; then
  445.         DIALOG=Xdialog
  446.     fi
  447.    
  448.     for i in $PKGS ; do
  449.         CHOICES="$CHOICES $i . off"
  450.     done
  451.    
  452.     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
  453.     all of the Puppy files are in a compressed read-only file named 'puppy.sfs'\n
  454.     or 'wary_500.sfs' (or some similar name). So you can't actually delete these\n
  455.     files. However, if you want to remaster the live-CD to create your own custom\n
  456.     Puppy (see Setup menu), you can use this little program to 'pretend' to delete\n
  457.     them -- and they will really be gone in the remastered CD. So, for example if\n
  458.     you remove SeaMonkey, it will be gone in the remastered CD thus reducing the\n
  459.     size of the .iso live-CD file.\n\n
  460.     Technical note: the lists of builtin files is at /root/.packages/builtin_files,\n
  461.     the list of builtin pkgs is in /root/.packages/woof-installed-packages.")" \
  462.     $MENUOPT "$(gettext 'Select packages to remove (be careful):')" 0 0 0 $CHOICES`
  463.     if [ ! "$PKG" ];then
  464.         exit
  465.     fi
  466.    
  467.     PKG=$(echo "$PKG" | sed 's%/% %g')
  468.     /usr/lib/gtkdialog/box_yesno --warning "Remove builtin pkg(s)" \
  469.         "Please confirm that you want to delete the following pkg(s):" "" \
  470.         "<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..."
  471.     if [ $? -ne 0 ] ; then
  472.         exit
  473.     fi
  474.    
  475.     #--
  476.     REMOVED_PKGS=''
  477.     #--
  478.    
  479.     for i in $PKG
  480.     do
  481.         DEP_OF=`grep "+${i}" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 2 |tr "\n" " "`
  482.         if [ "$DEP_OF" ] ; then
  483.             DESCR="`grep "|${i}|" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 10`"
  484.             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
  485.     ${DESCR}\n
  486.     $(gettext 'For information only, these are dependencies of') '${i}':\n
  487.     `grep "|${i}|" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 9 | sed -e 's%^+%%' -e 's%,+% %g'`
  488.     \n
  489.     `eval_gettext \"Warning, removing '\\\${i}' _may_ break the following packages:\"`
  490.     $DEP_OF
  491.     \n
  492.     $(gettext 'Continue?')" 0 0
  493.             if ! [ $? -eq 0 ];then
  494.                 continue
  495.             fi
  496.         fi
  497.         remove_builtin_pkg $i || continue
  498.         REMOVED_PKGS="$REMOVED_PKGS $i"
  499.     done
  500. fi
  501.  
  502. [ "$REMOVE_BUILTIN_fixmenus" = yes ] && fix_menus
  503.  
  504. if [ "$REMOVE_BUILTIN_use_gui" = yes ]; then
  505.     if [ "${REMOVED_PKGS}" ] ; then
  506.         /usr/lib/gtkdialog/box_ok "Remove builtin pkg(s)" info  \
  507.             "The following packages were removed:" "" "<b>${REMOVED_PKGS}</b>"
  508.     fi
  509. fi
  510. ### END ###
  511.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement