#!/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.
#131122 support xz compressed pets (see installpkg.sh, pet2tgz)
#131123 changed default to gzip, will change it back in a year or 2
#141011 add command line args for unattended builds
#160524 rerwin: correct search for top category in hierarchy.
#160803 rerwin: correct pet.specs path; sync compression default; simplify parameter parsing.
#160919 01micko: add -C option to change compression default.

case $(uname -m) in
	arm*) comp=gz ;; # default at first run
	*)    comp=xz ;; # default at first run
esac

if [ -f "$HOME/.dir2pet.cfg" ] ; then
	. $HOME/.dir2pet.cfg
fi
# set default compression
set_comp() {
    echo "Please set your default compression file format."
    echo "Compression determines the size of the end package. 'xz' is"
    echo "smaller but is slower to decompress, whereas 'gz' is the"
    echo "opposite. If you have an old, slow machine, 'gz' is recommended."
    echo "However, if you have a faster machine, 'xz' is probably a"
    echo "better choice. If you don't know, it will be safe just to press"
    echo "'Enter'."
    echo
    echo "Press 'Enter' to use the current $comp compression or press"
    echo "any prinatble key character and 'Enter' to change."

    case $comp in
        xz)newcomp=gz;;
        gz)newcomp=xz;;
    esac
    read setcomp
    [ -z "$setcomp" ] && echo "comp=$comp" > $HOME/.dir2pet.cfg ||\
    echo "comp=$newcomp" > $HOME/.dir2pet.cfg
    exit 0
}

usage() {
    echo '
Usage: dir2pet [option]... [<name-of-dir>]

Create a compressed file containing the content of a directory

Options:
  -h, --help			   show this help
  -C, --change-compression-default Change the default compression
				   (gz or xz, currently '"$comp"')

--------------------------------------------------------------------

Basic:
  <name-of-dir>	the directory, not path, to be packaged.
		THIS IS THE PACKAGE.
		(Omit if specified with -p option)

  EXAMPLE:
    # dir2pet my_fun_game-0.1-i486

--------------------------------------------------------------------

Advanced:
  -s		   skip all questions - for package build scripts
  -p=<name-of-dir> directory to be packaged. THIS IS THE PACKAGE.

The following options are only useful with the "-s" option.
For best results, use as many as possible:

Compression: (use only one, default if none chosen is \"$comp\")
  -x	use xz (high) compression
  -g	use gz (low) compression

Non-compulsory:
  -k      keep pet.specs / do not overwrite it
  -n                    no distro information
  -i="<repo> [version]" override default distro info
                        -i="noarch"
                        -i="debian stretch"
                        -i=""
  -w="<description>" quoted description of the application
  -d=<+deps>	     comma-delimited dependencies, with prepended
		     "+" sign
  -c=<Category>	     category of application, for package manager -
		     allowed categories:
			BuildingBlock (default)
			Desktop
			System
			Setup
			Utility
			Filesystem
			Graphic
			Document
			Business
			Personal
			Network
			Internet
			Multimedia
			Fun

  EXAMPLE
    # dir2pet -x -s -w="fun game" -d=+gtk+,+ffmpeg,+cairo -p=my_fun_game-0.1-i486
' #160803 160919
}

#parameters
while [ $# -gt 0 ]; do #160803
    case $1 in
        -x) comp=xz ;;
        -g) comp=gz ;;
        -k) keep_pet_specs=1 ;;
        -s) skip=1 ;;
        -w=*) desc="`echo "$1"|cut -d '=' -f2`" ;;
        -d=*) deps=`echo $1|cut -d '=' -f2`;;
        -c=*) category=`echo $1|cut -d '=' -f2`;;
        -p=*) directory=`echo $1|cut -d '=' -f2`;;
        -C|--change-compression-default) set_comp;; #160919
        [a-zA-Z]*)dir=$1;; #160803
        -n) nodistro=1 ;;
        -i=*) distro_info=`echo $1|cut -d '=' -f2`;;
        -h|--help|"") usage; exit;;
        *) echo "Invalid argument: $1"; usage; exit;;
    esac
    shift #also decrements $# 160803
done

ISDIR=$dir
[ -z "$ISDIR" ] && dir=$directory

if [ ! -d "$dir" ];then
 usage
 [ "$dir" ] && echo 'Error: Directory "'${dir}'" not found in '`pwd`
 exit
fi

ADIR=$dir
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 $DIRPKG/$BASEPKG/pet.specs ];then #160803
 #new: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|
 #optionally on the end: compileddistro|compiledrelease|repo| (fields 11,12,13)
 PETSPECS="`cat $DIRPKG/$BASEPKG/pet.specs | head -n 1`" #160803
 IFS="|" read -r F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 <<< "$PETSPECS"
 DB_pkgname="$F1"
 DB_nameonly="$F2"
 NAMEONLY="$DB_nameonly"
 DB_version="$F3"
 DB_pkgrelease="$F4"
 DB_category="$F5"
 PUPCATEGORY="$DB_category"
 DB_size="$F6"
 DB_path="$F7"
 PUPPATH="$DB_path" #100201
 DB_fullfilename="$F8"
 DB_dependencies="$F9"
 PUPOFFICIALDEPS="$DB_dependencies"
 DB_description="$F10"
 PUPMENUDESCR="$DB_description"
 DB_compileddistro="$F11"
 DB_compiledrelease="$F12"
 ARCHDEPENDENT="${DB_compileddistro}|${DB_compiledrelease}"
 DB_repo="$F13"
 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 "-"`"

# comp
if [ "$skip" != 1 ];then
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 an icon is required for the menu."
    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"
    case $comp in
        xz)default=xz char="g" other=gzip;;
        gz)default=gzip char="x" other=xz;;
    esac
    echo "to use the default \"$default\" compression or \"$char\" then ENTER for "
    echo "\"$other\" compression."
    echo -n "Press ENTER key or \"$char\" and ENTER to continue: "
    read goforit
    case $goforit in
    x)EXT="xz" 
    echo
    echo "xz compression chosen." 
    sleep 1;;
    g)EXT="gz"
    echo
    echo "gzip compression chosen." 
    sleep 1;;
    *)EXT="${default:0:2}" ;;
    esac #131122, 131123, 140715
else
    EXT=$comp
fi
# end comp
. /etc/xdg/menus/hierarchy #has PUPHIERARCHY variable.

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

if [ "$skip" != 1 ];then
    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."
     echo "It is preferred that you specify the full path."
     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
     echo
    fi

    #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.${EXT} 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}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
     echo "Icon=${PUPICON16}" >> $DIRPKG/$BASEPKG/usr/share/applications/${NAMEONLY}.desktop
     echo "Comment=${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
    # if it's a _DEV pkg.. it can't be ARCHINDEPENDENT
    case "${BASEPKG}" in *"_DEV"*) ARCHINDEPENDENT='no' ;; esac
    [ "$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
fi

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

if [ "$skip" != 1 ];then
    [ "${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 -e "[ ,]$xPUPCATEGORY[ ,]" -e "[ ,]$xPUPCATEGORY\$" | cut -f 1 -d ' ' | head -n 1`" #160524
    [ "${TOPCAT}" = "" ] && TOPCAT="EMPTY"
    [ "${PUPOFFICIALDEPS}" = "" ] && PUPOFFICIALDEPS="EMPTY"
    [ -z "${PUPMENUDESCR}" ] && PUPMENUDESCR="EMPTY"
    [ "${PUPPATH}" = "" ] && PUPPATH="EMPTY" #100201
    [ "${DEFREPO}" = "" ] && DEFREPO="EMPTY" #100201
    echo "a${NAMEONLY}" "b${TOPCAT}" "c${PUPOFFICIALDEPS}" "d${PUPMENUDESCR}" "e$BASEPKG" "f$SIZEK" "g$PUPPATH" "h$ARCHDEPENDENT" "i$DEFREPO"
    petspec "${NAMEONLY}" "${TOPCAT}" "${PUPOFFICIALDEPS}" "${PUPMENUDESCR}" "$BASEPKG" "$SIZEK" "$PUPPATH" "$ARCHDEPENDENT" "$DEFREPO" #100201
    if [ $? -ne 0 ];then
     echo "Aborted creation of PET package."
     exit
    fi

elif [ "$keep_pet_specs" ] && [ -f $DIRPKG/$BASEPKG/pet.specs ] ; then
	echo -n # don't overwrite pet specs
else
    . /etc/DISTRO_SPECS
    PUPOFFICIALDEPS="$deps"
    TOPCAT="$category"
    [ -z "$TOPCAT" ] && TOPCAT=BuildingBlock
    PUPMENUDESCR="$desc"
    [ -z "${PUPMENUDESCR}" ] && PUPMENUDESCR="No description provided"
    xPATTERN="s%${NAMEONLY}%%"
    VERSION="`echo -n "$BASEPKG" | sed -e "$xPATTERN" -e 's%^\\-%%'`"
	if [ "$nodistro" ] ; then
		COMPAT=""
		V=""
	elif [ "$distro_info" ] ; then
		read COMPAT V <<EOF
$distro_info
EOF
	else
		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
		# if it's a _DEV pkg.. it can't be ARCHINDEPENDENT
		case "${BASEPKG}" in *"_DEV"*) ARCHINDEPENDENT='no' ;; esac
		[ "`find $DIRPKG/$BASEPKG -maxdepth 6 -type f -name '*.a' -o -type f -name 'lib*.so*' -o -type f -name '*.la'`" != "" ] && ARCHINDEPENDENT='no'
		if [ "$ARCHINDEPENDENT" = "no" ] ; then
			COMPAT=$DISTRO_BINARY_COMPAT
			V=$DISTRO_COMPAT_VERSION
		fi
	fi
	#echo $COMPAT ; echo $V ; exit #debug
    echo "$BASEPKG|${NAMEONLY}|$VERSION||$TOPCAT|$SIZEK||${BASEPKG}.pet|$deps|$PUPMENUDESCR|$COMPAT|$V||" > /tmp/petspec_db_entry
fi

# /tmp/petspec_db_entry is creater either by /usr/sbin/petspec
# or this script (-s)
cat /tmp/petspec_db_entry | tail -n 1 > $DIRPKG/$BASEPKG/pet.specs

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

echo
echo "Creating package..."
tar -c -f $DIRPKG/${BASEPKG}.tar $DIRPKG/$BASEPKG/
sync
case $EXT in
xz)xz -z -6 -e -T 0 $DIRPKG/${BASEPKG}.tar ;;
gz)gzip --best $DIRPKG/${BASEPKG}.tar ;;
esac
TARBALL="$DIRPKG/${BASEPKG}.tar.${EXT}"

echo
echo "File $DIRPKG/${BASEPKG}.tar.${EXT} created. Now converting to .pet..."
FULLSIZE="`stat -c %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}/"
case $EXT in
xz)echo "# xz -z -6 -e -T 0 ${BASEPKG}.tar" ;;
gz)echo "# gzip --best ${BASEPKG}.tar" ;;
esac
echo "# tgz2pet ${BASEPKG}.tar.${EXT}"
echo
echo "dir2pet exited."

###END###