#!/bin/sh
#nicOS-Package-Extractor:  Utility for Puppy Linux (c) Copyright 2023, amethyst.

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

MSG1="`gettext \"Utility to 'extract' packages from the running Puppy filesystem.\"`"
D=/tmp/.packages
WrkDir="/tmp/extract"
rm -r $WrkDir
sleep 1
mkdir $WrkDir

gtkdialog-splash -close never -placement top -fontsize large -bg goldenrod -text " $(gettext 'Please wait, processing...') " &
XPID=$!

rm -r "$D"
sleep 1
mkdir -p "$D"
cp -a /root/.packages/* "$D"
mv -f "$D"/*/* "$D"
tmpfile=`mktemp`

for file in "$D"/*; do
  while read -r line; do
    [ -f "$line" ] && printf "%s\n" "$line"
  done <"$file" >"$tmpfile"
  mv "$tmpfile" "$file"
done

find "$D" -mindepth 1 -empty -delete
rm -d "$D"/*/* 

kill $XPID

 extract_pkg() {
	PKG=$1
	cd /
	for x in `cat $D/$PKG`; do [ -d $x ] && MYPATH="$x" || cp --parents -a "${MYPATH}/$x" $WrkDir; done
			 
}

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 --left --stdout --icon /usr/local/lib/X11/pixmaps/extract.png --backtitle "${MSG1}" --title "$(gettext 'nicOS-Package-Extractor')" --help "$(gettext "All packages accounted for in the packages folder are listed here.\n

Technical note 1:  the lists of all installed package files are normally located at /root/.packages or /var/packages.\n
Technical note 2:  the list of all installed packages is normally located at /root/.packages or /var/packages.\n

You can select more than one package in which case the contents of the packages will be merged.\n

This application reassembles all the files of a package as listed in its package file.  It does not include any dependencies.\n

The files of the selected package(s) will be copied from the running filesystem and rebuilt into an sfs file called 'Extracted.sfs'.
Rename it as you wish.")" \
$MENUOPT "$(gettext 'Select packages to extract')" 0 0 12 $CHOICES`
if [ ! "$PKG" ];then
	exit
fi

PKG=$(echo "$PKG" | sed 's%/% %g')

for i in $PKG
do
	DEP_OF=`grep "+${i}" /root/.packages/woof-installed-packages |cut -d "|" -f 2 |tr "\n" " "`
	if [ "$DEP_OF" ] ; then
		DESCR="`grep "|${i}|" /root/.packages/woof-installed-packages |cut -d "|" -f 10`"
		Xdialog --left --screen-center --backtitle "$(gettext 'Confirm that you want to extract') '${i}'" --title "$(gettext 'Extract installed 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}|" /root/.packages/woof-installed-packages |cut -d "|" -f 9 | sed -e 's%^+%%' -e 's%,+% %g'`
\n
$DEP_OF
\n
$(gettext 'Continue?')" 0 0
		if ! [ $? -eq 0 ];then
			continue
		fi
	fi
	extract_pkg $i || continue
	EXTRACTED_PKGS="$EXTRACTED_PKGS $i"
done

if [ "${EXTRACTED_PKGS}" ] ; then
	/usr/lib/gtkdialog/box_ok "Extract packages" info  \
		"The following packages were extracted:" "" "<b>${EXTRACTED_PKGS}</b>"
fi

mksquashfs $WrkDir /Extracted.sfs -comp gzip
rm $WrkDir -r
rm -r "$D" 
rox -s /Extracted.sfs
  
exit
 
####THE END######


