s243a icon

remove_builtin.sh

s243a | PRO | 02/14/21 08:45:49 PM UTC | 0 ⭐ | 976 👁️ | Never ⏰ | []
text |

11.08 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
 REMOVE_BUILTIN_fixmenus=no
REMOVE_BUILTIN_prompt=no
CHROOT_UPDATES=no
REMOVE_BUILTIN_use_gui=no
CHROOT_DIR="${CHROOT_DIR:-/mnt/+aufs+devsave+test_save+ext2/Kali/kalipup_CLI.tar.gz.extracted/kalipup/puppy_upupgg+d_20.10.sfs.extracted}"
 [ "`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 -u15 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 15< <( cat "$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 "Removing $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
			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
  [ "$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