#!/bin/sh
#Barry Kauler (c) Copyight 2007,2009 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#This script will create a PET package.
#Just create a directory with the name of the package, for example 'abiword-0.5.6'
#and put what you want in it.
#It should have abiword-0.5.6/usr/share/applications/abiword.desktop if the 
#application requires a menu entry, but if there isn't one this script will ask
#some simple questions and create one.
#This script will also create abiword-0.5.6/pet.specs, which has
#some information useful to the Puppy Package Manager.
#w476, w478 fixes to work with petspec.
#w482 if preexisting pet.specs, read fields from it.
#100201 improve reading of pet.specs
#100303 fix bug detection arch-dependent pkg.
#130305 rerwin: ensure package tmp directory, if any, has all permissions.

if [ ! $1 ];then
 echo "It is required to invoke this script like this:
# dir2pet abiword-0.5.6
where abiword-0.5.6 is a directory in the same directory
in which this terminal window is open. 'abiword-0.5.6' is
just an example, and contains subdirectories and files
that will become the PET package."
 exit
fi

if [ ! -d $1 ];then
 echo "$1 must be a directory"
 exit
fi

ADIR=$1
MYPID=${$}

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

#w482 directory may already have a pet.specs, reuse it...
NAMEONLY=""
PUPMENUDESCR=""
PUPOFFICIALDEPS=""
PUPCATEGORY=""
PUPPATH="" #100201
ARCHDEPENDENT="yes" #100201
DEFREPO="" #100201
if [ -f ${1}/pet.specs ];then
 #new: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|
 #optionally on the end: compileddistro|compiledrelease|repo| (fields 11,12,13)
 PETSPECS="`cat ${1}/pet.specs | head -n 1`"
 DB_pkgname="`echo -n "$PETSPECS" | cut -f 1 -d '|'`"
 DB_nameonly="`echo -n "$PETSPECS" | cut -f 2 -d '|'`"
 NAMEONLY="$DB_nameonly"
 DB_version="`echo -n "$PETSPECS" | cut -f 3 -d '|'`"
 DB_pkgrelease="`echo -n "$PETSPECS" | cut -f 4 -d '|'`"
 DB_category="`echo -n "$PETSPECS" | cut -f 5 -d '|'`"
 PUPCATEGORY="$DB_category"
 DB_size="`echo -n "$PETSPECS" | cut -f 6 -d '|'`"
 DB_path="`echo -n "$PETSPECS" | cut -f 7 -d '|'`"
 PUPPATH="$DB_path" #100201
 DB_fullfilename="`echo -n "$PETSPECS" | cut -f 8 -d '|'`"
 DB_dependencies="`echo -n "$PETSPECS" | cut -f 9 -d '|'`"
 PUPOFFICIALDEPS="$DB_dependencies"
 DB_description="`echo -n "$PETSPECS" | cut -f 10 -d '|'`"
 PUPMENUDESCR="$DB_description"
 DB_compileddistro="`echo -n "$PETSPECS" | cut -f 11 -d '|'`"
 DB_compiledrelease="`echo -n "$PETSPECS" | cut -f 12 -d '|'`"
 ARCHDEPENDENT="${DB_compileddistro}|${DB_compiledrelease}"
 DB_repo="`echo -n "$PETSPECS" | cut -f 13 -d '|'`"
 DEFREPO="$DB_repo"
fi

#difficult task, separate package name from version part... 
#not perfect, some start with non-numeric version info...
[ "$NAMEONLY" = "" ] && 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 "-"`"

clear
echo "Welcome to the 'dir2pet' script."
echo "This will convert a directory into a PET package. Example, you have a "
echo "directory named 'abiword-0.5.6' and inside that you place whatever is"
echo "needed for the package, for example usr/local/bin/abiword (the executable)"
echo "and usr/share/applications/abiword.desktop (the XDG menu file)."
echo "Whatever the packages needs, though don't worry if there is no .desktop"
echo "file as this script will ask some simple questions and optionally create"
echo "one. The package only needs a .desktop file if a menu entry is to be"
echo "created, and also a 16x16 pixel icon is required for the menu."
echo "It is preferred that the icon be an xpm image and the default location"
echo "for these is at /usr/local/lib/X11/mini-icons/, but it can be anywhere."
echo
echo "The directory $BASEPKG must separate name of the package and"
echo "version number with a dash. Ex: abiword-0.5.6"
echo
echo "The package may optionally have post-install and post-remove scripts,"
echo "named 'pinstall.sh' and 'puninstall.sh' placed at the top directory."
echo "(to create official PETs for the Unleashed suite, see its README file"
echo " for more information how to create these scripts properly)"
echo
echo "If any of the above needs to be further sorted out, you can quit this"
echo "script right now by pressing CTRL-C, otherwise just press ENTER."
echo -n "Press ENTER key to continue: "
read goforit

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

DESKTOPFILE=""
ADESKTOPFILE="`find $ADIR -type f -name \*.desktop`"
[ "$ADESKTOPFILE" != "" ] && DESKTOPFILE="yes"

echo
echo -en "\\033[1;31mStep 1" #red
echo -e "\\033[0;39m"
if [ "$DESKTOPFILE" = "" ];then
 echo "Currently there is no .desktop file (they are usually at usr/share/applications"
 echo "or usr/local/share/applications), so perhaps this package is not supposed to"
 echo "have a menu entry? You can optionally create one though..."
 echo "If you know that the ${BASEPKG} application does not require"
 echo "a menu entry, press the ENTER key only."
 echo "If a menu entry is required, type any printable character then ENTER."
 echo "(if in doubt, just press ENTER key)"
 echo -n "Type a printable character or just ENTER key: "
 read YESMENU
 [ "$YESMENU" = "" ] && DESKTOPFILE="ignore"
else #w476
 echo "A .desktop file was found here:"
 echo "$ADESKTOPFILE"
 echo "So this application will have a menu entry."
 echo "If you want to change the .desktop file in any way, open it in a text editor"
 echo "right now, before proceeding and make any changes you want."
 echo "In particular, check that the icon exists, and that 'Category' entry fits"
 echo "into Puppy's menu hierarchy (see file /etc/xdg/menus/hierarchy)."
 echo "After satisfying yourself that the .desktop file is ok, press the ENTER key"
 echo "to continue this script."
 echo "Or, type 'ignore' to build pet pkg as if there is no .desktop file."
 echo "Or, type 'new' if you would like this script to ask a series of"
 echo -n "questions and rebuild the .desktop file from scratch: "
 read NEWMENU
 [ "$NEWMENU" = "ignore" ] && DESKTOPFILE="ignore" #w478
 if [ "$NEWMENU" = "new" ];then
  for ONEDESKTOP in $ADESKTOPFILE
  do
   BASEDESK="`basename $ONEDESKTOP .desktop`"
   DIRDESK="`dirname $ONEDESKTOP`"
   echo "Moving ${DIRDESK}/${BASEDESK}.desktop to /tmp"
   mv -f ${DIRDESK}/${BASEDESK}.desktop /tmp/
  done
  DESKTOPFILE=""
 fi
fi

PUPAPPLICATION=""
PUPEXECUTABLE=""
PUPICON16=""
PUPAPPLICATION=""
if [ "$DESKTOPFILE" = "yes" ];then #w476
 #get some info out of it...
 FIRSTDESKTOPFILE="`echo -n "$ADESKTOPFILE" | head -n 1`"
 PUPCATEGORY="`cat $FIRSTDESKTOPFILE | grep '^Categories=' | cut -f 2 -d '='`"
 PUPEXECUTABLE="`cat $FIRSTDESKTOPFILE | grep '^Exec=' | cut -f 2 -d '='`"
 PUPICON16="`cat $FIRSTDESKTOPFILE | grep '^Icon=' | cut -f 2 -d '='`"
 PUPMENUDESCR="`cat $FIRSTDESKTOPFILE | grep '^Comment=' | cut -f 2 -d '='`"
 [ "$PUPMENUDESCR" = "" ] && PUPMENUDESCR="`cat $FIRSTDESKTOPFILE | grep '^Name=' | cut -f 2 -d '='`"
 [ "$PUPMENUDESCR" = "" ] && PUPMENUDESCR="`cat $FIRSTDESKTOPFILE | grep '^GenericName=' | cut -f 2 -d '='`"
fi

if [ "$DESKTOPFILE" = "" ];then
 echo
 echo -en "\\033[1;31mStep 1B" #red
 echo -e "\\033[0;39m"
 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 -en "\\033[1;31mStep 1C" #red
 echo -e "\\033[0;39m"
 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 "(of course, if you need to specify the path here, it is"
 echo " the path AFTER the package is installed)"
 echo
 echo -n "Enter [path]executable: "
 read PUPEXECUTABLE

 echo 
 echo -en "\\033[1;31mStep 1D" #red
 echo -e "\\033[0;39m"
 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 package gfnrename-0.4 installs"
 echo "a 16x16 icon at /usr/local/gfnrename-0.4/icon.xpm"
 echo "(of course, if you need to specify the path here, it is"
 echo " the path AFTER the package is installed)"
 echo "IT MUST BE A 16x16 PIXEL ICON, NO BIGGER!"
 echo
 echo -n "Please type [path]icon: "
 read PUPICON16

 echo 
 echo -en "\\033[1;31mStep 1E" #red
 echo -e "\\033[0;39m"
 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

if [ "$PUPMENUDESCR" = "" ];then #w476
 echo
 echo -en "\\033[1;31mDescription" #red
 echo -e "\\033[0;39m"
 echo "Please enter a description of 1-3 words."
 if [ "$DESKTOPFILE" = "" ];then
  echo "This must be extremely short. as it will appear in the window"
  echo "manager menu entry immediately after the application name."
 fi
 echo "This may be used for various purposes, such by PETget for package"
 echo "management purposes."
 echo "Example for Abiword: powerful wordprocessor"
 echo
 echo -n "Type the VERY SHORT description (without quotes): "
 read PUPMENUDESCR
#else
# echo "A description of the package has been extracted:"
# echo " '${PUPMENUDESCR}'"
# echo "This will be placed in the package database entry. Press ENTER if ok,"
# echo -n "or type an alternative very short description: "
# read NEWDESCR
# if [ "$NEWDESCR" != "" ];then
#  PUPMENUDESCR="$NEWDESCR"
# fi
fi

echo

#if pkg is a split-off, already has a known dependency...
DEPBASE="";DEPNOTE=""
[ ! "`echo -n "$NAMEONLY" | grep '_DEV'`" = "" ] &&  DEPBASE="+`echo -n "$NAMEONLY" | sed -e 's/_DEV//g'`"
[ ! "`echo -n "$NAMEONLY" | grep '_DOC'`" = "" ] &&  DEPBASE="+`echo -n "$NAMEONLY" | sed -e 's/_DOC//g'`"
[ ! "`echo -n "$NAMEONLY" | grep '_NLS'`" = "" ] &&  DEPBASE="+`echo -n "$NAMEONLY" | sed -e 's/_NLS//g'`"
if [ ! "$DEPBASE" = "" ];then
 DEPNOTE="NOTE5: It is strongly suggested that you at least enter ${DEPBASE}
       the main package 
"
fi

if [ "$PUPOFFICIALDEPS" = "" ];then
 echo
 echo -en "\\033[1;31mDependencies" #red
 echo -e "\\033[0;39m"
 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,+gtkdialog3"
 echo "Each package name is preceded by a '+' and delimited by a ','."
 echo "NOTE1: that 'gtkdialog3' requires the GTK libraries, but it is not"
 echo "       necessary to specify sub-dependencies, as if 'gtkdialog3'"
 echo "       needs to be installed it has its own dependency list."
 echo "NOTE2: You can lookup the dependency-list of each package in the"
 echo "       /root/.packages/Packages-* database files"
 echo "NOTE3: it is not required to specify package version numbers,"
 echo "       VERSION NUMBERS NOT YET SUPPORTED BY PACKAGE MANAGER"
 echo "NOTE4: If you don't know what to specify, just press ENTER key"
 echo "       (the package manager will still do some basic dependency checking)"
 echo "$DEPNOTE"
 echo -n "Type dependency-list: "
 read PUPOFFICIALDEPS
fi

echo
echo -en "\\033[1;31mGUI window" #red
echo -e "\\033[0;39m"

#create tarball...
rm -f $DIRPKG/${BASEPKG}.tar 2>/dev/null
rm -f $DIRPKG/${BASEPKG}.tar.gz 2>/dev/null
rm -f $DIRPKG/${BASEPKG}.pet 2>/dev/null
if [ "$DESKTOPFILE" = "" ];then
 mkdir -p $DIRPKG/$BASEPKG/usr/share/applications
 echo '[Desktop Entry]' > $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo 'Encoding=UTF-8' >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "Name=${PUPAPPLICATION} ${PUPMENUDESCR}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "Icon=${PUPICON16}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "Comment=${PUPAPPLICATION} ${PUPMENUDESCR}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "Exec=${PUPEXECUTABLE}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "Terminal=false" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "Type=Application" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "Categories=${PUPCATEGORY}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo "GenericName=${PUPAPPLICATION} ${PUPMENUDESCR}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
 echo
 echo "File $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop created."
 echo
fi

#100201 determine if pkg is ditro-independent (scripts only)...
ARCHINDEPENDENT='yes'
for ONEEXEC in `find $DIRPKG/$BASEPKG -maxdepth 6 -type f -perm -o+x`
do
 [ -f $ONEEXEC ] && [ "`file $ONEEXEC | grep ' ELF '`" != "" ] && ARCHINDEPENDENT='no'
done
[ "`find $DIRPKG/$BASEPKG -maxdepth 6 -type f -name '*.a' -o -type f -name 'lib*.so*' -o -type f -name '*.la'`" != "" ] && ARCHINDEPENDENT='no' #100303
#[ "`echo "${BASEPKG}" | grep '_DEV'`" != "" ] && ARCHINDEPENDENT='no' #100303
[ "$ARCHINDEPENDENT" = "yes" ] && ARCHDEPENDENT='no'

echo "Press ENTER key to bring up a GUI window that will help you to create"
echo "a database entry for the package. This will be shown in a text editor"
echo "for saving somewhere, also written to file 'pet.specs' inside the pkg." 
echo -n "Press ENTER: "
read enternow

SIZEK="`du -s -k $DIRPKG/$BASEPKG | cut -f 1`" #w476

[ "${PUPCATEGORY}" = "" ] && PUPCATEGORY="EMPTY"
#if PUPCATEGORY is in format 'entry1;entry2;' extract only 'entry2'...
xPUPCATEGORY="`echo -n "$PUPCATEGORY" | tr ';' ' ' | tr -s ' ' | sed -e 's% $%%' | rev | cut -f 1 -d ' ' | rev`"
TOPCAT="`echo "$PUPHIERARCHY" | grep "$xPUPCATEGORY" | cut -f 1 -d ' ' | head -n 1`"
[ "${TOPCAT}" = "" ] && TOPCAT="EMPTY"

[ "${PUPOFFICIALDEPS}" = "" ] && PUPOFFICIALDEPS="EMPTY"
[ "${PUPMENUDESCR}" = "" ] && PUPMENUDESCR="EMPTY"
[ "${PUPPATH}" = "" ] && PUPPATH="EMPTY" #100201
[ "${DEFREPO}" = "" ] && DEFREPO="EMPTY" #100201
petspec "${NAMEONLY}" "${TOPCAT}" "${PUPOFFICIALDEPS}" "${PUPMENUDESCR}" "$BASEPKG" "$SIZEK" "$PUPPATH" "$ARCHDEPENDENT" "$DEFREPO" #100201

if [ $? -ne 0 ];then
 echo "Aborted creation of PET package."
 exit
fi

[ -d $DIRPKG/$BASEPKG/tmp ] && chmod 1777 $DIRPKG/$BASEPKG/tmp #130305 rerwin.

cat /tmp/petspec_db_entry | tail -n 1 > $DIRPKG/$BASEPKG/pet.specs

echo
echo "Creating package..."
tar -c -f $DIRPKG/${BASEPKG}.tar $DIRPKG/$BASEPKG/
sync
gzip $DIRPKG/${BASEPKG}.tar
#rmdir $DIRPKG/$BASEPKG
TARBALL="$DIRPKG/${BASEPKG}.tar.gz"

echo
echo "File $DIRPKG/${BASEPKG}.tar.gz created. Now converting to .pet..."
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
echo "${BASEPKG}.pet has been created. Finished."
echo
echo "If you look in ${BASEPKG} you will see the new '.specs' file."
if [ "$DESKTOPFILE" = "" ];then
 echo "And in $BASEPKG/usr/share/applications/ the new '.desktop' file."
fi
echo "Directory $BASEPKG is now configured correctly as a PET package"
echo "and in future you do not need to go through this script again."
echo "You could manually edit the files if required, and create another"
echo ".pet package just by doing this:"
echo "# tar -c -f ${BASEPKG}.tar ${BASEPKG}/"
echo "# gzip ${BASEPKG}.tar"
echo "# tgz2pet ${BASEPKG}.tar.gz"
echo
echo "dir2pet exited."

###END###
