#!/bin/sh

#w467
###Woof###
#this script no longer used. .pup pkgs no longer supported.
echo
echo ".pup packages are no longer supported in Puppy,"
echo "This conversion script is no longer supported either."
exit
##########

#convert a .pup file to .pet...
#passed param is file to be converted.
#this works for md5sum returning 8-bit characters!

#note, this script does not really convert a DotPup package into a PET package.
#The .pup is just placed inside a directory and the directory is made into a
#tarball then into a .pet file. The PETget package manager will recognise
#this and will launch the DotPup package installer.
#However, there is some enhanced capability, for example if the
#.pup does not register in /root/.packages/ then PETget will do it.

sync
[ ! $1 ] && exit
APUP=$1
MYPID=${$}
CURRDIR="`pwd`"

#only accept .pup files...
[ "`echo -n "$APUP" | grep '\\.pup$'`" = "" ] && exit 1
[ ! -f "$APUP" ] && exit 1

chmod +w "$APUP" #make it writable.

#split APUP path/filename into components...
BASEPKG="`basename $APUP .pup`"
DIRPKG="`dirname $APUP`"
[ "$DIRPKG" = "/" ] && DIRPKG=""

[ -e $DIRPKG/$BASEPKG ] && exit 1

. /etc/xdg/menus/hierarchy #has PUPHIERARCHY variable.

#find out what are official Unleashed packages...
#cat /root/.packages/

#want to find out if dotpup has a .desktop file inside...
#awkward, as .tar.bz2 files inside .zip file... have to unzip it...
#mkdir /tmp/tmp${MYPID}
#cp -a "$APUP" /tmp/tmp${MYPID}/
#unzip /tmp/tmp${MYPID}/${BASEPKG}.pup
#DESKTOPFILE="`find /tmp/tmp${MYPID} -type f -name \*.desktop`"
#if [ "$DESKTOPFILE" = "" ];then
# if [ -f FILESYSTEM.tbz ];then
#  DESKTOPFILE="`tar --list -f FILESYSTEM.tbz | grep '\.desktop$'`"
# fi
#fi
#if [ "$DESKTOPFILE" = "" ];then
# #see if can find icon, 
#fi

#if dotpup already has .desktop file, use it ...reluctantly, as looking at some,
#they are wrong: menu icon not 16x16, wrong executable
DESKTOPFILE=""
[ ! "`unzip -l "$APUP" | grep 'FILESYSTEM\.tbz'`" = "" ] && DESKTOPFILE="maybe"
[ ! "`unzip -l "$APUP" | grep '\.desktop$'`" = "" ] && DESKTOPFILE="yes"

echo
echo "This script will convert ${BASEPKG}.pup into ${BASEPKG}.pet"
echo
echo "If you know that the ${BASEPKG} application does not require a menu"
echo "entry, please type 'no' (without the quotes) now. If a menu entry is"
echo "required, just press ENTER key only."
echo -n "Type 'no' or just ENTER key: "
read NOMENU
[ ! "$NOMENU" = "" ] && DESKTOPFILE="ignore"

if [ "$DESKTOPFILE" = "maybe" ];then
 echo
 echo "It seems that ${BASEPKG}.pup does probably contain a .desktop"
 echo "file -- this file has the information needed for creating a"
 echo "menu entry when the package is installed. This 'pup2pet' script"
 echo "does not uncompress the ${BASEPKG}.pup file so is unable to"
 echo "verify 100% that the '.desktop' file exists."
 echo "If you are willing to open up the ${BASEPKG}.pup file yourself,"
 echo "you may confirm this and if the file does not exist, then type the"
 echo "letters 'no' followed by ENTER here, otherwise just press ENTER key."
 echo "NOTE: the .desktop file in some DotPups has incorrect entries, so"
 echo "      you can choose 'no' here even if there is one, to create a new"
 echo "      .desktop file, but you must open up the DotPup to determine"
 echo "      some relevent parameters (menu icon and executable) -- this"
 echo "      script will provide some guidance in doing this."
 echo
 echo -n "Type 'no' (no .desktop) or just ENTER: "
 read NODESKTOPFILE
 [ "$NODESKTOPFILE" = "no" ] && DESKTOPFILE=""
fi

PUPAPPLICATION=""
PUPCATEGORY=""
PUPEXECUTABLE=""
PUPICON16=""
PUPAPPLICATION=""
if [ "$DESKTOPFILE" = "" ];then
 echo
 echo "The .pup package may (or may not) have installation scripts"
 echo "that register the installed files with the PETget package"
 echo "manager and create an entry in the window manager menus."
 echo "However, the information may not be in a format that is"
 echo "compatible with PETget. Therefore, please answer the following"
 echo "questions, that will override whatever the .pup install"
 echo "script wants to create. You will need to know some basic"
 echo "details about the DotPup package if it requires a menu entry"
 echo "such as /path/filenames of the icon and the executable."
 echo "if you are converting a DotPup and you are not familiar"
 echo "with its internal details, if you click on it and install it"
 echo "then you should be able to see what gets installed -- ROX-Filer"
 echo "has a nice feature, highlights recently-changed/created files"
 echo "in bold letters."
 echo
 echo -n "Press ENTER key to continue: "
 read yeahdoit

 echo
 echo "EXTRA NOTES1: You can expand a DotPup file and examine its"
 echo "contents. Create a directory, copy the .pup into it, open a"
 echo "terminal, type 'unzip filename.pup'. Internally it may contain"
 echo ".tbz files which you can expand: 'tar -jxf filename.tbz'"
 echo "EXTRA NOTES2: If you make a mistake while running this script,"
 echo "you could run it again, or alternatively you can open up the"
 echo "created PET package and edit the settings then close it again."
 echo "The steps are:"
 echo "# pet2tgz pkgname.pet      (converts .pet to a .tar.gz)"
 echo "# tar -zxf pkgname.tar.gz  (expands the tarball)"
 echo " ...inside you will find a text-files, edit them."
 echo "# tar -c -f pkgname.tar pkgname/   (create a tar file)"
 echo "# gzip pkgname.tar         (create .tar.gz tarball)"
 echo "# tgz2pet pkgname.tar.gz   (convert .tar.gz to .pet)"
 echo
 echo -n "Press ENTER key to continue: "
 read yeahdoit

 echo
 echo "Please type the category in which you want the application"
 echo "to create a window manager menu entry. The official Puppy"
 echo "has a menu hierarchy as follows:"
 echo "('X-' categories are unofficial, Puppy-specific)"
 echo "$PUPHIERARCHY"
 echo
 echo -n "Type one word from the CATEGORIES column: "
 read PUPCATEGORY
 
 echo
 echo "Please enter the name of the executable. If it is in the"
 echo "executable-search-path, namely /bin, /sbin, /usr/bin,"
 echo "/usr/sbin, /root/my-applications/bin or /usr/local/bin,"
 echo "then you only need to enter the name of the executable"
 echo "not the path. Example: mtpaint"
 echo "Many DotPups do install an executable into one of these"
 echo "directories, usually /usr/local/bin. Example: Gfnrename-0.4"
 echo "installs an executable at /usr/local/Gfnrename-0.4/gfnrename"
 echo "but also at /usr/local/bin/gfnrename and it is intended that"
 echo "the latter be used to run the program."
 echo
 echo -n "Enter [path]executable: "
 read PUPEXECUTABLE
 echo
 
 echo "Please enter the name of the icon that is to be used in"
 echo "the window manager menu entry. This must be a 16x16 pixel"
 echo ".xpm file (.png okay but not suitable all window managers)."
 echo "If this icon is located at /usr/local/lib/X11/mini-icons"
 echo "then there is no need to specify the path."
 echo "If the icon is located elsewhere then the full path must"
 echo "be provided. An example: the DotPup Gfnrename-0.4 installs"
 echo "a 16x16 icon at /usr/local/Gfnrename-0.4/icon.xpm"
 echo "IT MUST BE A 16x16 PIXEL ICON, NO BIGGER!"
 echo
 echo -n "Please type [path]icon: "
 read PUPICON16
 
 echo
 echo "Please enter the name of the application, as you wish it to"
 echo "appear in the menu. It will be the first word in the menu entry."
 echo "Example: Abiword"
 echo
 echo -n "Type application name: "
 read PUPAPPLICATION
fi

echo
echo "Please enter a description of 1-3 words. This must be extremely"
echo "short, as it will appear in the window manager menu entry"
echo "(if there is one) immediately after the application name."
echo "This may also be used for other purposes, such as in the"
echo "/root/.packages/alienpackages.txt file (an entry in this"
echo "file is used by PETget for package management purposes)."
echo "Example for Abiword: wordprocessor"
echo
echo -n "Type the VERY SHORT description (without quotes): "
read PUPMENUDESCR

echo
echo "List of official packages built-in to Puppy:"
cat /root/.packages/packages.txt | grep '" on "' | cut -f 2 -d '"' | cut -f 1 -d '-' | tr '\n' ' ' #' Geany fix.
echo
echo
echo "Listed above are the packages built-in to Puppy. These are all"
echo "in the pup_xxx.sfs file (on the CD if you booted from CD)."
echo -n "Press ENTER key to continue: "
read yabbado
echo
echo "Here are the extra official packages that are available:"
cat /root/.packages/packages.txt | grep '" off "' | cut -f 2 -d '"' | cut -f 1 -d '-' | tr '\n' ' ' #' Geany fix.
echo
echo
echo "The PET packages listed above are from the Unleashed suite that were"
echo "not chosen to be built into the pup_xxx.sfs file. However, they may"
echo "be individually installed at any time."
echo -n "Press ENTER key to continue: "
read yabbado
echo
echo "Please enter a dependency-list for the PET package that is now being"
echo "created. Packages already built-in to Puppy do not need to be"
echo "explicitly named as dependencies (except a cut-down barebones version"
echo "of Puppy may not have all of these built in, so you may have to"
echo "think of the worst-case situation)."
echo "How to enter this dependency-list is shown by an example: the package"
echo "'pupdvdtool-0.5b' has the following dependency list:"
echo "+vamps,+vobcopy,+ffmpeg,+dvdauthor,+gtkdialog"
echo "Each package name is preceded by a '+' and delimited by a ','."
echo "NOTE1: that 'gtkdialog' requires the GTK libraries, but it is not"
echo "       necessary to specify sub-dependencies, as if 'gtkdialog'"
echo "       needs to be installed it has its own dependency list."
echo "NOTE2: You can lookup the dependency-list of each package in"
echo "       /root/.packages/packages.txt"
echo "NOTE3: it is not required to specify package version numbers,"
echo "       unless the package requires a non-official dependency."
echo "       (any versioned dependency automatically means it is unofficial)"
echo "NOTE4: If you don't know what to specify, just press ENTER key"
echo "       (PETget will still do some basic dependency checking)."
echo
echo -n "Type dependency-list: "
read PUPOFFICIALDEPS

echo
echo "A PET package has the option of not registering with PETget, the"
echo "package manager, when it installs. This is useful for bugfix and"
echo "patch PETs. The normal response here is just press ENTER key, as"
echo "you would normally want to register with PETget so that the package"
echo "can be uninstalled."
echo "Do you want the PET package to register with PETget?"
echo -n "Just hit ENTER for 'yes', otherwise type 'no': "
read PETREGISTER
[ "$PETREGISTER" = "" ] && PETREGISTER="yes"

#difficult task, separate package name from version part... 
#not perfect, some start with non-numeric version info...
NAMEONLY="`echo -n "$BASEPKG" | sed -e 's/\-[0-9].*$//g'`"
#...if that fails, do it the old way...
[ "$NAMEONLY" = "$BASEPKG" ] && NAMEONLY="`echo "$BASEPKG" | cut -f 1 -d "-"`"

#embed .pup inside a directory and create tarball...
rm -f $DIRPKG/${BASEPKG}.tar
rm -f $DIRPKG/${BASEPKG}.tar.gz
rm -f $DIRPKG/${BASEPKG}.pet
mkdir $DIRPKG/$BASEPKG
if [ "$DESKTOPFILE" = "" ];then
 echo '[Desktop Entry]' > $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo 'Encoding=UTF-8' >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "Name=${PUPAPPLICATION} ${PUPMENUDESCR}" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "Icon=${PUPICON16}" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "Comment=${PUPAPPLICATION} ${PUPMENUDESCR}" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "Exec=${PUPEXECUTABLE}" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "Terminal=false" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "Type=Application" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "Categories=${PUPCATEGORY}" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
 echo "GenericName=${PUPAPPLICATION} ${PUPMENUDESCR}" >> $DIRPKG/$BASEPKG/${NAMEONLY}.desktop
fi
#echo "PUPAPPLICATION='${PUPAPPLICATION}'" > $DIRPKG/$BASEPKG/${BASEPKG}.pet.specs
echo "PETMENUDESCR='${PUPMENUDESCR}'" > $DIRPKG/$BASEPKG/${BASEPKG}.pet.specs
echo "PETOFFICIALDEPS='${PUPOFFICIALDEPS}'" >> $DIRPKG/$BASEPKG/${BASEPKG}.pet.specs
echo "PETREGISTER='${PETREGISTER}'" >> $DIRPKG/$BASEPKG/${BASEPKG}.pet.specs
mv $DIRPKG/${BASEPKG}.pup $DIRPKG/$BASEPKG/
tar --remove-files -c -f $DIRPKG/${BASEPKG}.tar $DIRPKG/$BASEPKG/
gzip $DIRPKG/${BASEPKG}.tar
rmdir $DIRPKG/$BASEPKG
TARBALL="$DIRPKG/${BASEPKG}.tar.gz"

FULLSIZE="`stat --format=%s ${TARBALL}`"
MD5SUM="`md5sum $TARBALL | cut -f 1 -d ' '`"
echo -n "$MD5SUM" >> $TARBALL
sync
mv -f $TARBALL $DIRPKG/${BASEPKG}.pet
sync

echo "${BASEPKG}.pet has been created. Finished."

###END###
