s243a icon

move_builtin.sh (Draft #1)

s243a | PRO | 03/19/21 12:50:13 AM UTC (Edited) | 0 ⭐ | 1667 👁️ | Never ⏰ | []
Bash |

13.65 KB

|

None

|

0 👍

/

0 👎

#!/bin/bash
#Barry Kauler Dec. 2010, license GPL v3 /usr/share/doc/legal.
#based on a script by technosaurus, Dec. 2010.
#Modified by s243a to work with an alternative root and for better cli support. 
 
set -x
CWD="$PWD"
REMOVE_BUILTIN_fixmenus=no
REMOVE_BUILTIN_prompt=no
CHROOT_UPDATES=no
REMOVE_BUILTIN_use_gui=no
CHROOT_DIR="${CHROOT_DIR:-"$(echo "$CWD/puppy_fossapup64_9.5.sfs.extracted")"}"
TARGET_DIR="${TARGET_DIR:-"$(echo "$CWD/fossapup64_9.5_basesfs_corecli")"}"
#D=$CHROOT_DIR/root/.packages/builtin_files
D_rel=/var/packages/builtin_files-sml
D2_rel=/var/packages/builtin_files-sml_corecli
 
[ -z "$D_rel" ] && D_rel=/root/.packages/builtin_files
D="$CHROOT_DIR$D_rel"
D="$(realpath "$D")"
[ ! -d "$D" ] && D=$CHROOT_DIR/var/packages/builtin_files
D2="$TARGET_DIR$D2_rel"
mkdir -p "$D2"
D2="$(realpath "$D2")"
[ ! -d "$D" ] && D2=$TARGET_DIR/var/packages/builtin_files
#/var/packages/builtin_files-sml
set +x
read -p "press enter to continue"
set -x
mkdir -p "$TARGET_DIR"
function mk_parrent_dir(){
  parrent="$(dirname $1)"
  if  [ ${#parrent} -le 1 ]; then 
    return 0
  else [ ! -d "$TARGET_DIR$parrent" ];
    if [ ! -z "`which cpio`" ]; then
        ( cd "$CHROOT_DIR"
          echo "./$1" | \
            cpio -pdu "$TARGET_DIR" )
    else
      mkdir -p "$TARGET_DIR/$1"
    fi          
  fi
}
if [ -z "$BUILTIN_ACITON" ]; then
     if [ -z "$TARGET_DIR" ]; then
      ACTION_MESSAGE="removing"
    else
      ACTION_MESSAGE="moving"
    fi 
else
  case "$BUILTIN_ACITON" in
  rm) ACTION_MESSAGE="removing"; ;;
  mv) ACTION_MESSAGE="moving"; ;;
  cp) ACTION_MESSAGE="copying"; ;;
  esac
fi
function builtin_action(){
  local a_action
  if [ -z "$BUILTIN_ACITON" ]; then
    if [ -z "$TARGET_DIR" ]; then
      a_action="rm"
    else
      a_action="mv"
    fi
  else
    a_action="$BUILTIN_ACITON"
  fi
      case "$a_action" in
      rm)
        rm $CHROOT_DIR$1
        ;;
      mv) 
        mk_parrent_dir "$1"
        mv "$CHROOT_DIR$1" "$TARGET_DIR$1" 
        ;;
      cp) #TODO: use cpio instead of cp. 
        if [ ! -z "`which cpio`" ]; then
         ( cd "$CHROOT_DIR"
              echo "./$1" | \
                cpio -pdu "$TARGET_DIR" )    
        else
          cp -a "$CHROOT_DIR$1" "$TARGET_DIR$1"
        fi 
        ;;
      esac
  
}
#[ -f $D/$PKG ] && rm $D/$PKG
function d_action(){
  local a_action
  if [ -z "$BUILTIN_ACITON" ]; then
    if [ -z "$TARGET_DIR" ]; then
      a_action="rm"
    else
      a_action="mv"
    fi
  else
    a_action="$BUILTIN_ACITON"
  fi
      case "$a_action" in
      rm)
        rm $D/$1
        ;;
      mv) 
        #mk_parrent_dir "$1"
        mv "$D/$1" "$D2/$1" 
        ;;
      cp) #TODO: use cpio instead of cp. 
        if [ ! -z "`which cpio`" ]; then
         ( cd "$CHROOT_DIR$D"
              echo "./$1" | \
                cpio -pdu "$TARGET_DIR$D2" )    
        else
          cp -a "$D/$1" "$D2/$1"
        fi 
        ;;
      esac
    
}
#echo CHROOT_DIR="CHROOT_DIR=$CHROOT_DIR"
#set +x 
#read -p "Press Enter to continue"
#set -x
 
[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505
 
export TEXTDOMAIN=remove_builtin
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
 
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)\"`"
FIXMENU='no'
 
#D=$CHROOT_DIR/root/.packages/builtin_files
#D="$(realpath "$D")"
#[ ! -d "$D" ] && D=$CHROOT_DIR/var/packages/builtin_files
 
#===========================================================
process_remove_builtin_list() {
    # count
    #set -x 
    local pkglist="$1" COUNT=$(wc -l < "$1") COUNTER=0
    mkdir -p /tmp/deb-build/
    #exec 15<> /tmp/deb-build/fd_15
    
    
    # process
    while read -r p; do
        echo "Processing: $p"
        p=${p%%#*}; eval set -- $p; p="$1"
        [ -z $p ] && continue
        
        # commands
        COUNTER=$((COUNTER+1))      
        echo $COUNTER of $COUNT "$@"        
        case $p in
            #%exit) break ;;
            #%dpkg)
            #   echo Switching to dpkg
            #   [ -z "$WITHOUT_DPKG" ] && do_install() { dpkg_install; } ||
            #   do_install() { dpkgchroot_install; } ;;
            #%dpkgchroot)
            #   echo Switching to dpkgchroot
            #   do_install() { dpkgchroot_install; } ;;
            #%bootstrap)
            #   echo Switching to bootstrap
            #   do_install() { bootstrap_install; } ;;
            #%bblinks)
            #   shift # $1-nousr
            #   echo Installing busybox symlinks ...
            #   install_bb_links "$@" ;;
            #%makesfs)
            #    set -x
            #    #unbind_ALL
            #    wait_until_unmounted || exit
            #   shift # $1-output $@-squashfs params
            #   echo Creating $1 ...
            #   make_sfs "$@" ;;
            #%remove)
            #   shift # $1-pkgname, pkgname, ...
            #   remove_pkg "$@" ;;
            #%addbase)
            #   echo Installing base rootfs ...
            #   [ "$BASE_ARCH_PATH" ] && install_from_dir $BASE_ARCH_PATH base-arch core                
            #   install_from_dir $BASE_CODE_PATH base core ;;
            #%addpkg)
            #    set -x
            #   shift # $1-pkgname, pkgname ...
            #   forced=0
            #   PKGSECTION="optional"
            #   while [ "$1" ]; do
            #       case "$1" in
            #       --forced)
            #         forced=1
            #         ;;
            #       --category=*)
            #         PKGSECTION="${1#'--category='}";
            #         ;;
            #       *)
            #         ! [ -d $EXTRAPKG_PATH/$1 ] && shift && continue
            #         echo Installing extra package "$1"  ...
            #         if [ $forced -eq 1 ]; then
            #           install_from_dir $EXTRAPKG_PATH/$1 $1 $PKGSECTION force
            #         else
            #           install_from_dir $EXTRAPKG_PATH/$1 $1 $PKGSECTION
            #         fi
            #         ;;
            #       esac
            #       shift
            #   done
            #   set +x
            #    ;;
            #%importpkg)
            #  shift
            #  set -x
            #  import_from_dir $EXTRAPKG_PATH/$1
            #  set +x
            #    ;;
            #%dummy)
            #    set -x
            #   shift # $1-pkgname, pkgname ...
            #   install_dummy "$@" 
            #   set +x 
            #   ;;
            #%dpkg_configure)
            #   shift # $@-configure flags
            #   DEBIAN_FRONTEND=noninteractive chroot $CHROOT_DIR /usr/bin/dpkg --configure "$@" ;;
            #%lock)
            #   shift # $1-pkgname, pkgname ...
            #   lock_pkg "$@" ;;
            #%cutdown)
            #    set -x
            #   shift # $@ various cutdown options
            #   cutdown "$@" ;;
            #%import)
            #   shift # $@ dirs to import
            #   import_dir "$@" ;;
            #   
            #### filesystem operations
            #%symlink)
            #   shift # $1 source $2 target
            #   rm -f $CHROOT_DIR/$2
            #   ln -s $1 $CHROOT_DIR/$2
            #   ;;
            #%rm)
            #   shift # $@ files to remove
            #   while [ "$1" ]; do
            #       rm -rf $CHROOT_DIR/$1
            #       shift
            #   done
            #   ;;
            #%mkdir)
            #   shift # $@ dirs to make
            #   while [ "$1" ]; do
            #       mkdir -p $CHROOT_DIR/$1
            #       shift
            #   done
            #   ;;
            #%touch)
            #   shift # $@ files to create
            #   while [ "$1" ]; do
            #       > $CHROOT_DIR/$1
            #       shift
            #   done
            #   ;;
            #%chroot)
            #   shift # $@ commands
            #   chroot $CHROOT_DIR "$@"
            #   ;;
            #
            # anything else - install package
            *)
                #get_pkg_info $p
                #[ -z "$PKG" ] && echo Cannot find ${p}. && continue
                #download_pkg || { echo Download $p failed. && exit 1; }
                #install_pkg "$2" || { echo Installation of $p failed. && exit 1; }
                remove_builtin_pkg "$p"         
                ;;
        esac        
    done < "$pkglist" #< $pkglist
    #exec 15>&-
    #rm /tmp/deb-build/fd_14
    return 0
}
 
function remove_builtin_pkg() {
    PKG="$1"
    local NO_PKG=false
    if [ -z "$PKG" ] ; then
        return 1
    fi
    if [ ! -f $D/$PKG ] ; then
        echo "$PKG does not exist.." >&2
        #return 1 #TODO: we need alternative to "return 1" here
        NO_PKG=true
    else
      echo "$ACTION_MESSAGE $PKG"
      if grep -q '\.desktop$' ${D}/${PKG}; then
        [ "$REMOVE_BUILTIN_fixmenus" = yes ] && FIXMENU='yes' #101222
      fi
      (
        while read file
        do
            #rm "$CHROOT_DIR$file" 1>&2
            builtin_action "$file" 1>&2
            echo "${file%/*}" >&2 #get dir
        done < $D/$PKG
      ) > /tmp/remove_builtin_dirs$$
      #-- remove empty directories
      sort -ur /tmp/remove_builtin_dirs$$ | \
      while read dir
      do
        while [ 1 ] ; do
            rmdir "$dir" 2>/dev/null
            dir=${dir%/*} #dirname $dir
            if [ -z "$dir" ] ; then
                break
            fi
        done
      done
    #-- 
  fi
  PKGFILES="$D/$PKG"
 
  if [ "$CHROOT_UPDATES" = yes ] && [ "$NO_PKG" = false ]; then
    if [ "`grep '/usr/share/glib-2.0/schemas' $PKGFILES`" != "" ];then
     [ -e /usr/bin/glib-compile-schemas ] && glib-compile-schemas /usr/share/glib-2.0/schemas
    fi
    
    if [ "`grep '/usr/lib/gio/modules' $PKGFILES`" != "" ];then
     [ -e /usr/bin/gio-querymodules ] && gio-querymodules /usr/lib/gio/modules
    fi
    
    if [ "`grep ''\.desktop$'' $PKGFILES`" != "" ];then
     rm -f "$CHROOT_DIR/usr/share/applications/mimeinfo.cache"
     [ -e /usr/bin/update-desktop-database ] && update-desktop-database /usr/share/applications
    fi
    
    if [ "`grep '/usr/share/mime' $PKGFILES`" != "" ];then
     [ -e /usr/bin/update-mime-database ] && update-mime-database /usr/share/mime
    fi
    
    if [ "`grep '/usr/share/icons/hicolor' $PKGFILES`" != "" ];then
     [ -e /usr/bin/gtk-update-icon-cache ] && gtk-update-icon-cache /usr/share/icons/hicolor
    fi
    
    if [ "`grep '/usr/lib/gdk-pixbuf' $PKGFILES`" != "" ];then
     gdk-pixbuf-query-loaders --update-cache
    fi
    
    if [ "`grep '/usr/lib/gconv' $PKGFILES`" != "" ];then
     iconvconfig
    fi
    
    if [ "`grep '/usr/lib/pango' $PKGFILES`" != "" ];then
     pango-querymodules --update-cache
    fi
 
 
    for gtkver in '1.0' '2.0' '3.0'
    do
     if [ "`grep "/usr/lib/gtk-$gtkver" $PKGFILES | grep "/immodules"`" != "" ];then
      [ -e /usr/bin/gtk-query-immodules-$gtkver ] && gtk-query-immodules-$gtkver --update-cache
     fi
    done
    
    if [ "`grep '/usr/share/fonts' $PKGFILES`" != "" ];then
     fc-cache -f
    fi
    
    KERNVER="$(uname -r)"
    
    if [ "`grep "/lib/modules/$KERNVER" $PKGFILES`" != "" ];then
     depmod -a
    fi
  fi    
    
  #[ -f $D/$PKG ] && rm $D/$PKG
  d_action "$PKG"
  [ "$NO_PKG" = false ] && rm /tmp/remove_builtin_dirs$$
  sed -i "\%|${PKG}|%d" "$CHROOT_DIR/root/.packages/woof-installed-packages"
  line="$(cat "$CHROOT_DIR/root/.packages/DISTRO_PKGS_SPECS" | grep "^yes|${PKG}|")"
  pkgs_str=$(echo $line | cut -d'|' -f3)
  pkgs=(${pkgs_str//,/" "})
  for a_pkg in "${pkgs[@]}"; do
    sed -i "\%|${a_pkg}|%d" "$CHROOT_DIR/root/.packages/woof-installed-packages"
  done 
  return 0
}
 
fix_menus() {
    if [ "$FIXMENU" = "yes" ];then #101222
        fixmenus
        if [ "$DISPLAY" ] ; then
            if [ "`pidof jwm`" != "" ] ; then
                jwm -reload || jwm -restart
                sleep 1
            fi
        fi
    fi
}
 
#===========================================================
#                   COMMAND LINE
#============================================================
 
case $1 in 
  -l|-list|--list)
    ls -1 $D
    exit ;;
  --pkg-list)
    shift
    process_remove_builtin_list "$@"
    exit ;;     
esac
 
if [ "$1" ] ; then
    for i in $@
    do
        remove_builtin_pkg $i
    done
    [ "$REMOVE_BUILTIN_fixmenus" = yes ] && fix_menus
    exit $?
fi
 
#===========================================================
#                    GUI
#============================================================
 
 
if [ REMOVE_BUILTIN_use_gui = yes ]; then
    PKGS=`ls -1 $D`
    DIALOG="dialog --aspect 10"
    MENUOPT="--checklist"
    REP=/tmp/$(basename $0).txt
    
    if [ "$DISPLAY" ] ; then
        DIALOG=Xdialog
    fi
    
    for i in $PKGS ; do
        CHOICES="$CHOICES $i . off"
    done
    
    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
    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.")" \
    $MENUOPT "$(gettext 'Select packages to remove (be careful):')" 0 0 0 $CHOICES`
    if [ ! "$PKG" ];then
        exit
    fi
    
    PKG=$(echo "$PKG" | sed 's%/% %g')
    /usr/lib/gtkdialog/box_yesno --warning "Remove builtin pkg(s)" \
        "Please confirm that you want to delete the following pkg(s):" "" \
        "<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..."
    if [ $? -ne 0 ] ; then
        exit
    fi
    
    #--
    REMOVED_PKGS=''
    #--
    
    for i in $PKG
    do
        DEP_OF=`grep "+${i}" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 2 |tr "\n" " "`
        if [ "$DEP_OF" ] ; then
            DESCR="`grep "|${i}|" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 10`"
            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
    ${DESCR}\n
    $(gettext 'For information only, these are dependencies of') '${i}':\n
    `grep "|${i}|" "$CHROOT_DIR/root/.packages/woof-installed-packages" |cut -d "|" -f 9 | sed -e 's%^+%%' -e 's%,+% %g'`
    \n
    `eval_gettext \"Warning, removing '\\\${i}' _may_ break the following packages:\"`
    $DEP_OF
    \n
    $(gettext 'Continue?')" 0 0
            if ! [ $? -eq 0 ];then
                continue
            fi
        fi
        remove_builtin_pkg $i || continue
        REMOVED_PKGS="$REMOVED_PKGS $i"
    done
fi
 
[ "$REMOVE_BUILTIN_fixmenus" = yes ] && fix_menus
 
if [ "$REMOVE_BUILTIN_use_gui" = yes ]; then
    if [ "${REMOVED_PKGS}" ] ; then
        /usr/lib/gtkdialog/box_ok "Remove builtin pkg(s)" info  \
            "The following packages were removed:" "" "<b>${REMOVED_PKGS}</b>"
    fi
fi
### END ###
 

Comments