#!/bin/sh
#(c) copyright Barry Kauler June 2012
#i wrote another script 'createpackages' that extracts all of the binary pkgs out of a Gentoo distro.
#(i had a sd card image for the raspi).
#'createpets' converts those binary pkgs into pet pkgs.

. /etc/xdg/menus/hierarchy #has PUPHIERARCHY variable.
CURRPATH="`pwd`"

WOOF_HOSTARCH='x86'
WOOF_TARGETARCH='arm'
NLS='_NLS'
EXE=''
DEV='_DEV'
DOC='_DOC'
DB_compileddistro='puppy'
DB_compiledrelease='ap6' #"Arm Puppy for armv6"
DBFILE="Packages-${DB_compileddistro}-${DB_compiledrelease}-official"

#paths ***SET THESE TO SUIT YOURSELF***
BINDIR='0-armv6-gentoo-bin-pkgs' #location of the binary pkgs
DESTDIR="${CURRPATH}/0-armv6-pets-from-bin-pkgs" #where the pet pkgs are to be created. Note: need absolute path.
[ -d $DESTDIR ] && rm -rf ${CURRPATH}/0-armv6-pets-from-bin-pkgs/*
TEMPLATES='packages-templates' #copy this dir out of woof, if running this script elsewhere.
FIND_CAT='./find_cat' #copy this executable out of woof.

[ ! -d $BINDIR ] && exit #sanity check.
mkdir -p $DESTDIR
[ -d sandbox2 ] && rm -rf sandbox2
mkdir -p sandbox2
echo -n "" > $DBFILE

#db fields: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|

for APKGNAME in `find ${BINDIR} -mindepth 1 -maxdepth 1 -type d | tr '\n' ' '`
do

 baseAPKGNAME="`basename $APKGNAME`"
 echo "Processing $baseAPKGNAME"
 destAPKGNAME="`echo -n "$baseAPKGNAME" | tr '[A-Z]' '[a-z]'`" #ex: abiword-1.2.3-r1
 [ -d ${DESTDIR}/${destAPKGNAME}-${DB_compiledrelease} ] && destAPKGNAME="${destAPKGNAME}-SECOND" #precaution.
 [ -d ${DESTDIR}/${destAPKGNAME}-SECOND-${DB_compiledrelease} ] && destAPKGNAME="${destAPKGNAME}-THIRD" #precaution.
 
 #cp -a -f $APKGNAME ${DESTDIR}/${destAPKGNAME}
 rm -rf sandbox2/* 2>/dev/null
 cp -a -f ${APKGNAME}/* sandbox2/
 sync
 
 NAMEONLY="$(echo -n "$destAPKGNAME" | sed -e 's%\-[0-9][0-9.\-].*%%')"
 vPTN="s%${NAMEONLY}\-%%"
 PKGVERSION="$(echo -n "$destAPKGNAME" | sed -e "$vPTN")"
 if [ ! "$PKGVERSION" ];then #destAPKGNAME=abiword-1 does not get split properly.
  NAMEONLY="$(echo -n "$destAPKGNAME" | sed -e 's%\-[0-9].*%%')"
  vPTN="s%${NAMEONLY}\-%%"
  PKGVERSION="$(echo -n "$destAPKGNAME" | sed -e "$vPTN")"
 fi
 GENERICNAME="$NAMEONLY"
 UPKGVERSION="$(echo -n "$PKGVERSION" | sed -e 's%\-r[0-9].*%%')" #ex: 2.3-r1 becomes 2.3
 
 if [ -f ${APKGNAME}/DESCRIPTION ];then
  DB_description="$(cat ${APKGNAME}/DESCRIPTION)"
 else
  DB_description='no description'
 fi
 
 DB_category="`${FIND_CAT} $NAMEONLY "$DB_description"`"
 
 #fix icon and category in .desktop file...
 #code from 2createpackages...
 DEFICON='Executable.xpm'
 CATEGORY="$DB_category"
 case $CATEGORY in
  Desktop)    CATEGORY='Desktop;X-Desktop' ; DEFICON='mini.window3d.xpm' ;;
  System)     CATEGORY='System' ; DEFICON='mini-term.xpm' ;;
  Setup)      CATEGORY='Setup;X-SetupEntry' ; DEFICON='so.xpm' ;;
  Utility)    CATEGORY='Utility' ; DEFICON='mini-hammer.xpm' ;;
  Filesystem) CATEGORY='Filesystem;FileSystem' ; DEFICON='mini-filemgr.xpm' ;;
  Graphic)    CATEGORY='Graphic;Presentation' ; DEFICON='image_2.xpm' ;;
  Document)   CATEGORY='Document;X-Document' ; DEFICON='mini-doc1.xpm' ;;
  Business)   CATEGORY='Business;X-Calculate' ; DEFICON='mini-calc.xpm' ;; #110821 Calculate is old name, now Business.
  Personal)   CATEGORY='Personal;X-Personal' ; DEFICON='mini-book2.xpm' ;;
  Network)    CATEGORY='Network' ; DEFICON='pc-2x.xpm' ;;
  Internet)   CATEGORY='Internet;X-Internet' ; DEFICON='pc2www.xpm' ;;
  Multimedia) CATEGORY='Multimedia;AudioVideo' ; DEFICON='Animation.xpm' ;;
  Fun)        CATEGORY='Fun;Game' ; DEFICON='mini-maze.xpm' ;;
  Develop)    CATEGORY='Utility' ; DEFICON='mini-hex.xpm' ;;
  Help)       CATEGORY='Utility' ; DEFICON='info16.xpm' ;;
 esac
 cPATTERN="s%^Categories=.*%Categories=${CATEGORY}%"
 iPATTERN="s%^Icon=.*%Icon=${DEFICON}%"
 for ONEDESKTOP in `find sandbox2/usr/share/applications sandbox2/usr/local/share/applications -type f -name '*.desktop' 2>/dev/null | tr '\n' ' '`
 do
  #find if category is already valid...
  CATFOUND="no"
  for ONEORIGCAT in `cat $ONEDESKTOP | grep '^Categories=' | head -n 1 | cut -f 2 -d '=' | tr ';' ' '`
  do
   oocPATTERN=' '"$ONEORIGCAT"' '
   [ "`echo "$PUPHIERARCHY" | tr -s ' ' | cut -f 3 -d ' ' | tr ',' ' ' | sed -e 's%^% %' -e 's%$% %' | grep "$oocPATTERN"`" != "" ] && CATFOUND="yes"
  done
  if [ "$CATFOUND" = "no" ];then
   sed -e "$cPATTERN" $ONEDESKTOP > /tmp/2createpackages-tmp
   mv -f /tmp/2createpackages-tmp $ONEDESKTOP
  fi
  #does the icon exist?... fix .desktop...
  ICON="`grep '^Icon=' $ONEDESKTOP | cut -f 2 -d '='`"
  if [ "$ICON" != "" ];then
   [ -e "sandbox2${ICON}" ] && continue #it may have a hardcoded path.
   [ -e "${ICON}" ] && continue #it may have a hardcoded path, look in running puppy.
   ICONBASE="`basename "$ICON"`"
   #first search where jwm looks for icons... check if paths exist...
   FNDICON=""
   [ -d sandbox2/usr/share/pixmaps ] && FNDICON="`find sandbox2/usr/share/pixmaps -maxdepth 1 -name $ICONBASE -o -name $ICONBASE.png -o -name $ICONBASE.xpm -o -name $ICONBASE.jpg -o -name $ICONBASE.jpeg -o -name $ICONBASE.gif -o -name $ICONBASE.svg | grep -i -E 'png$|xpm$|jpg$|jpeg$|gif$|svg$' | head -n 1`"
   [ ! "$FNDICON" ] && [ -d sandbox2/usr/local/lib/X11/mini-icons ] && FNDICON="`find sandbox2/usr/local/lib/X11/mini-icons -maxdepth 1 -name $ICONBASE -o -name $ICONBASE.png -o -name $ICONBASE.xpm -o -name $ICONBASE.jpg -o -name $ICONBASE.jpeg -o -name $ICONBASE.gif -o -name $ICONBASE.svg | grep -i -E 'png$|xpm$|jpg$|jpeg$|gif$|svg$' | head -n 1`"
   if [ "$FNDICON" ];then
    ICONNAMEONLY="`basename $FNDICON`"
    iPTN="s%^Icon=.*%Icon=${ICONNAMEONLY}%"
    sed -i -e "$iPTN" $ONEDESKTOP
    continue
   else
    #look elsewhere, including in running puppy... fix for parole /usr/share/parole/parole.png...
    FNDICON="`find sandbox2 /usr/share/icons /usr/local/share/pixmaps /usr/share/pixmaps -name $ICONBASE -o -name $ICONBASE.png -o -name $ICONBASE.xpm -o -name $ICONBASE.jpg -o -name $ICONBASE.jpeg -o -name $ICONBASE.gif -o -name $ICONBASE.svg  | sed -e 's%sandbox2%%' | grep -i -E 'png$|xpm$|jpg$|jpeg$|gif$|svg$' | head -n 1`"
    if [ "$FNDICON" ];then
     ICONNAMEONLY="`basename "$FNDICON"`"
     mkdir -p sandbox2/usr/share/pixmaps
     ln -snf "$FNDICON" sandbox2/usr/share/pixmaps/${ICONNAMEONLY}
     iPTN="s%^Icon=.*%Icon=${ICONNAMEONLY}%"
     sed -i -e "$iPTN" $ONEDESKTOP
     continue
    fi
   fi
   #substitute a default icon...
   sed -i -e "$iPATTERN" $ONEDESKTOP
  fi
 done
 
 #now process sandbox2 and create split rootfs, devx, docs, nls...
 cd sandbox2
 #now to split pkg into exe,dev,doc,nls components...
 #this will find all files and symlinks to files...
 find -L ./ -noleaf -type f | sed -e 's/^\.\///' |
 while read ONEFILE
 do
  #problem, coming here with paths that have symlinks...
  UPONE="`dirname "$ONEFILE"`"
  while [ "$UPONE" != "." -a "$UPONE" != "/" ];do
   [ -h "$UPONE" ] && continue 2
   UPONE="`dirname "$UPONE"`"
  done
  ONEBASE="`basename "$ONEFILE"`"
  ONEPATH="`dirname "$ONEFILE"`"
  [ "$ONEPATH" = "." ] && ONEPATH=""
  #strip the file...
  #but not if a cross-build (see file WOOFMERGEVARS), strip won't work...
  if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then
   if [ "`which strip`" != "" ];then #needs devx.
    if [ ! -h "$ONEFILE" ];then #make sure it isn't a symlink
     [ ! "`file "$ONEFILE" | grep 'ELF' | grep 'shared object'`" = "" ] && strip --strip-debug "$ONEFILE"
     [ ! "`file "$ONEFILE" | grep 'ELF' | grep 'executable'`" = "" ] && strip --strip-unneeded "$ONEFILE"
    fi
   fi
  fi
  sync
  ###NLS###
  #find out if this is an international language file...
  if [ "`echo -n "$ONEFILE" | grep --extended-regexp '/locale/|/nls/|/i18n/'`" != "" ];then
   if [ "`echo -n "$ONEFILE" | grep 'share/X11/locale/'`" = "" ];then #T2, need Xorg locale files in main pkg.
    [ "$NLS" = "_NULL" ] && continue #just dump it.
    mkdir -p "${DESTDIR}/${NAMEONLY}${NLS}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}"
    cp -af "$ONEFILE" "${DESTDIR}/${NAMEONLY}${NLS}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}" 2>/dev/null
    continue
   fi
  fi
  ###DOC###
  #find out if this is a documentation file...
  if [ ! "`echo -n "$ONEFILE" | grep --extended-regexp '/man/|/doc/|/doc-base/|/docs/|/info/|/gtk-doc/|/faq/|/manual/|/examples/|/help/|/htdocs/'`" = "" ];then
   [ "$DOC" = "_NULL" ] && continue #just dump it.
   mkdir -p "${DESTDIR}/${NAMEONLY}${DOC}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}"
   cp -af "$ONEFILE" "${DESTDIR}/${NAMEONLY}${DOC}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}" 2>/dev/null
   continue
  fi
  ###DEV###
  #find out if this is development file...
  if [ ! "`echo -n "$ONEFILE" | grep --extended-regexp 'X11/config/|/include/|/pkgconfig/|/aclocal|/cvs/|/svn/'`" = "" ];then
   mkdir -p "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}"
   cp -af "$ONEFILE" "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}" 2>/dev/null
   continue
  fi
  #find *.so symlink files...
  if [ -h "$ONEFILE" ];then #-h tests for symlink
   if [ ! "`echo -n "$ONEFILE" | grep '\.so$'`" = "" ];then
    mkdir -p "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}"
    cp -af "$ONEFILE" "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}" 2>/dev/null
    continue
   fi
  fi
  #find various config files...
  if [ ! "`echo -n "$ONEBASE" | grep --extended-regexp '\-config$|config.sh$|Conf.sh$'`" = "" ];then
   mkdir -p "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}"
   cp -af "$ONEFILE" "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}" 2>/dev/null
   continue
  fi
  #all .a and .la files... and any stray .m4 files...
  if [ ! "`echo -n "$ONEBASE" | grep --extended-regexp '\.a$|\.la$|\.m4$'`" = "" ];then
   mkdir -p "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}"
   cp -af "$ONEFILE" "${DESTDIR}/${NAMEONLY}${DEV}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}" 2>/dev/null
   continue
  fi
  ###EXE###
  #anything left over goes into the main 'executable' package...
  mkdir -p "${DESTDIR}/${NAMEONLY}${EXE}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}"
  cp -af "$ONEFILE" "${DESTDIR}/${NAMEONLY}${EXE}-${PKGVERSION}-${DB_compiledrelease}/${ONEPATH}" 2>/dev/null
 done
 cd ..

 #now do some post-processing using "template" found in 'packages-templates'...
 if [ -d ${TEMPLATES}/$GENERICNAME ];then
  echo " postprocessing with ${TEMPLATES}/$GENERICNAME"
  rm -rf /tmp/createpets-backup-pkg 2>/dev/null
  cp -a ${DESTDIR}/${NAMEONLY}-${PKGVERSION}-${DB_compiledrelease} /tmp/createpets-backup-pkg
  for TEMPLATEPATH in `find ${TEMPLATES}/$GENERICNAME -type d`
  do
   TEMPLATEFILES="`find $TEMPLATEPATH -maxdepth 1 -type f`"
   TEMPLATELINKS="`find $TEMPLATEPATH -maxdepth 1 -type l`"
   #some dirs in template renamed with "PKGVERSION" in the name...
   upPATTERN="s%PKGVERSION%${UPKGVERSION}%"
   ppPATTERN="s%${TEMPLATES}/${GENERICNAME}%${DESTDIR}/${NAMEONLY}-${PKGVERSION}-${DB_compiledrelease}%"
   TARGETPATH="`echo -n "$TEMPLATEPATH" | sed -e "$ppPATTERN" | sed -e "$upPATTERN"`"
   mkdir -p $TARGETPATH
   tPTN="s%${TEMPLATES}/${GENERICNAME}%/tmp/createpets-backup-pkg%"
   TMPPATH="`echo -n "$TEMPLATEPATH" | sed -e "$tPTN" | sed -e "$upPATTERN"`"
   
   #bit radical, but if dir in template has one or more files, truncate those in target...
   if [ ! -f $TEMPLATEPATH/PLUSEXTRAFILES ];then #marker-file, that all files in deb to be kept.
    tPATTERN="${DESTDIR}/"
    [ "$DESTDIR" = "" ] && exit #another paranoid precaution.
    [ "`echo "$TARGETPATH" | grep "$tPATTERN"`" = "" ]  && continue #paranoid precaution! (see next line)...
    #if [ "$TEMPLATEFILES" != "" ];then
     for ONEDEL in `find ${TARGETPATH} -maxdepth 1 -type f` #ignore symlinks.
     do
      #if file has any versioning info, do not delete...
      DELBASE="`basename $ONEDEL`"
	     #if dir has NOEXCEPTIONFILES then do not allow these exceptions...
      [ ! -f $TEMPLATEPATH/NOEXCEPTIONFILES ] && [ "`echo "$DELBASE" | grep -E '\.[0-9]*\.|\.[0-9]*$|\-[0-9]*\.|[0-9]\.so$|\.so\.[0-9]'`" != "" ] && continue
      ##sometimes regular file but template has symlink of same name (ex: /usr/bin/gnumeric). do not delete...
      #[ -L ${TEMPLATEPATH}/${DELBASE} ] && continue
      rm $ONEDEL
     done
    #fi
   fi
   
   #if file exists in template, non-0 copy it from template to target, 0-size copy from deb pkg to target...
   for FINALFILE in $TEMPLATEFILES
   do
    ALTTARGETPATH="$TARGETPATH"
    ALTTMPPATH="$TMPPATH"
    TMPLNAMEONLY="`basename $FINALFILE -FULL`" #coreutils & util-linux have some file-FULL.
    if [ ! -f $TMPPATH/$TMPLNAMEONLY ];then
     if [ "/tmp/createpets-backup-pkg" != "$TMPPATH" ];then #ignore top level.
      if [ "`echo -n "$TMPPATH" | grep -E "/root|/etc|/dev|/var/"`" = "" ];then #ignore if file in /root or /etc or /dev
       #if the target file is somewhere else, find it...
       ALTLOCATION="`find /tmp/createpets-backup-pkg -type f -name $TMPLNAMEONLY | head -n 1`"
       if [ "$ALTLOCATION" != "" ];then
        #ALTTARGETPATH="`dirname $ALTLOCATION | sed -e 's%^/tmp/%${DESTDIR}/%'`"
        [ "`file "$ALTLOCATION" | grep ' text'`" = "" ] && ALTTMPPATH="`dirname $ALTLOCATION`" #ignore text file.
       else
        #a hack, util-linux has 'rename.ul', presume ubuntu have renamed it so as not to conflict
        #with some other 'rename'....
        ALTLOCATION="`find /tmp/createpets-backup-pkg -type f -name ${TMPLNAMEONLY}.ul | head -n 1`"
        if [ "$ALTLOCATION" != "" ];then
         ALTTMPPATH="`dirname $ALTLOCATION`"
         TMPLNAMEONLY="$TMPLNAMEONLY"'.ul'
        #else
        # echo "WARNING: Not found in .deb: $FINALFILE"
        fi
       fi
      fi
     fi
    fi
    if [ -s $FINALFILE ];then
     #all non-0-size files must be copied from template to final pkg...
     cp -af $FINALFILE $ALTTARGETPATH/
    else
     #zero-size file. copy from the backup made in /tmp/createpets-backup-pkg to final pkg...
     TARGETNAMEONLY="`basename $FINALFILE`"
     if [ "`echo -n "$TARGETNAMEONLY" | grep "STARCHAR"`" != "" ];then
      #if template file has text STARCHAR in it, replace with wildcard ...
      globTARGETNAMEONLY="`echo -n "$TARGETNAMEONLY" | sed -e 's%STARCHAR%*%'`"
      cp -a --remove-destination ${ALTTMPPATH}/${globTARGETNAMEONLY} ${ALTTARGETPATH}/ 2>/dev/null
     else
      cp -a --remove-destination ${ALTTMPPATH}/${TMPLNAMEONLY} ${ALTTARGETPATH}/${TARGETNAMEONLY} 2>/dev/null
     fi
    fi
   done
   
   #prune target dirs that are not in template (unless PLUSEXTRADIRS file exists in path)...
   PLUSEXTRADIRS='no'
   TESTPATH="$TEMPLATEPATH"
   while [ "$TESTPATH" != "${TEMPLATES}" ];do
    if [ -f $TESTPATH/PLUSEXTRADIRS ];then
     PLUSEXTRADIRS='yes'
     break
    fi
    TESTPATH="`dirname $TESTPATH`"
   done
   if [ "$PLUSEXTRADIRS" = "no" ];then
    TEMPLATEDIRS="`find $TEMPLATEPATH -maxdepth 1 -type d | rev | cut -f 1 -d '/' | rev`"
	   #bug if 'PKGVERSION' is in template path...
    upPATTERN="s%PKGVERSION%${UPKGVERSION}%"
    xTEMPLATEDIRS="`echo "$TEMPLATEDIRS" | sed -e "$upPATTERN"`"
    for ONETARGETDIR in `find $TARGETPATH -mindepth 1 -maxdepth 1 -type d | rev | cut -f 1 -d '/' | rev`
    do
	    #precaution. dunno why, some pkgs in packages-woof are disappearing...
	    [ `echo -n "$TARGETPATH/$ONETARGETDIR" | sed -e 's%[^/]%%g' | wc -c` -le 1 ] && continue
     tPATTERN='^'"$ONETARGETDIR"'$'
     if [ "`echo "$xTEMPLATEDIRS" | grep "$tPATTERN"`" = "" ];then
      tPATTERN="${DESTDIR}/"
      [ "`echo "$TARGETPATH/$ONETARGETDIR" | grep "$tPATTERN"`" != "" ] && rm -rf $TARGETPATH/$ONETARGETDIR #test is paranoid precaution.
     fi
    done
   fi
  
   #if this dir in template has symlink(s), assume they are also required in target _EXE pkg...
   gnPATTERN="s%raw/${GENERICNAME}/%raw/${GENERICNAME}_DEV/%"
   DEVTARGETPATH="`echo -n "$TARGETPATH" | sed -e "$gnPATTERN"`"
   if [ ! -d $DEVTARGETPATH ];then
    gnPATTERN="s%raw/${GENERICNAME}/%raw/${GENERICNAME}_NULL/%"
    DEVTARGETPATH="`echo -n "$TARGETPATH" | sed -e "$gnPATTERN"`"
   fi
   if [ "$TEMPLATELINKS" != "" -a -d $DEVTARGETPATH ];then
    for ONEDEVLINK in `find $DEVTARGETPATH -maxdepth 1 -type l 2>/dev/null`
    do
     if [ "`readlink $ONEDEVLINK | grep '^/'`" != "" ];then #absolute link
      READLINK="`readlink $ONEDEVLINK`"
      [ ! -e ${TEMPLATES}/${GENERICNAME}${READLINK} ] && continue
     fi
     TARGETLINK="`basename $ONEDEVLINK`"
     [ ! -e $TARGETPATH/$TARGETLINK ] && [ ! -L $TARGETPATH/$TARGETLINK ] && cp -af ${ONEDEVLINK} ${TARGETPATH}/
    done
   fi
   #um, above not quite enough, do this too...
   if [ "$TEMPLATELINKS" != "" ];then
    for ONETEMPLATELINK in $TEMPLATELINKS
    do
     TARGETLINKNAME="`basename $ONETEMPLATELINK`"
     if [ ! -e $TARGETPATH/$TARGETLINKNAME ];then
      mkdir -p $TARGETPATH
      cp -a -f $TEMPLATEPATH/$TARGETLINKNAME $TARGETPATH/
     fi
    done
   fi
  done 
   
  #a last resort fixup, if 'FIXUPHACK' exists, execute it... DISABLE_POST_INSTALL_SCRIPT code moved up.
  #if [ -f ${DESTDIR}/$GENERICNAME/FIXUPHACK ];then
  if [ -f ${TEMPLATES}/$GENERICNAME/FIXUPHACK ];then #111210
   cp -af ${TEMPLATES}/$GENERICNAME/FIXUPHACK ${DESTDIR}/${NAMEONLY}${EXE}-${PKGVERSION}-${DB_compiledrelease}/
   #DPIS="`grep 'DISABLE_POST_INSTALL_SCRIPT' ${DESTDIR}/$GENERICNAME/FIXUPHACK`"
   export UPKGVERSION
   if [ "$EXE" = "_DEV" ];then
    #only have _DEV dir so this is where must run FIXUPHACK...
    mv -f ${DESTDIR}/$GENERICNAME/FIXUPHACK ${DESTDIR}/${NAMEONLY}_DEV-${PKGVERSION}-${DB_compiledrelease}/
    cd ${DESTDIR}/${NAMEONLY}_DEV-${PKGVERSION}-${DB_compiledrelease}/
   else
    cd ${DESTDIR}/${NAMEONLY}-${PKGVERSION}-${DB_compiledrelease}/
   fi
   sh ./FIXUPHACK
   cd $CURRPATH
   [ -f ${DESTDIR}/${NAMEONLY}-${PKGVERSION}-${DB_compiledrelease}/FIXUPHACK ] && rm -f ${DESTDIR}/${NAMEONLY}-${PKGVERSION}-${DB_compiledrelease}/FIXUPHACK
   [ -f ${DESTDIR}/${NAMEONLY}_DEV-${PKGVERSION}-${DB_compiledrelease}/FIXUPHACK ] && rm -f ${DESTDIR}/${NAMEONLY}_DEV-${PKGVERSION}-${DB_compiledrelease}/FIXUPHACK
  fi
 fi

 #for each created dir, create 'pet.specs' file, create pet pkgs...
 if [ -f ${APKGNAME}/DEPEND ];then
  DB_dependencies="$(cat ${APKGNAME}/DEPEND | tr ' ' '\n' | rev | cut -f 1 -d '/' | rev | cut -f 1 -d ':' | cut -f 1 -d '[' | sed -e 's%\-[0-9][0-9.\-].*%%' -e 's%\-[0-9]$%%' -e 's%^%+%' | sed -e 's%$%,%' | sort -u | tr '\n' ' ' | tr -d ' ' | sed -e 's%,$%%' | tr '[A-Z]' '[a-z]')"
 else
  DB_dependencies=''
 fi
 #db fields: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
 if [ -d ${DESTDIR}/${NAMEONLY}-${PKGVERSION}-${DB_compiledrelease} ];then
  DB_pkgname="${NAMEONLY}-${PKGVERSION}-${DB_compiledrelease}"
  DB_nameonly="${NAMEONLY}"
  DB_version="${PKGVERSION}-${DB_compiledrelease}"
  DB_size="`du -k -s ${DESTDIR}/${DB_pkgname} | cut -f 1`"
  DB_fullfilename="${DB_pkgname}.pet"
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" > ${DESTDIR}/${DB_pkgname}/pet.specs
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" >> $DBFILE
  DB_dependencies="+${NAMEONLY}"
  [ -f ${DESTDIR}/${DB_pkgname}/CATEGORY ] && rm -f ${DESTDIR}/${DB_pkgname}/CATEGORY
  [ -f ${DESTDIR}/${DB_pkgname}/DEPEND ] && rm -f ${DESTDIR}/${DB_pkgname}/DEPEND
  [ -f ${DESTDIR}/${DB_pkgname}/DESCRIPTION ] && rm -f ${DESTDIR}/${DB_pkgname}/DESCRIPTION
  [ -f ${DESTDIR}/${DB_pkgname}/RDEPEND ] && rm -f ${DESTDIR}/${DB_pkgname}/RDEPEND
  [ -f ${DESTDIR}/${DB_pkgname}/SIZE ] && rm -f ${DESTDIR}/${DB_pkgname}/SIZE
  #create pet pkg...
  cd $DESTDIR
  dir2tgz ${DB_pkgname}
  tgz2pet ${DB_pkgname}.tar.gz
  cd $CURRPATH
 fi
 if [ -d ${DESTDIR}/${NAMEONLY}_DEV-${PKGVERSION}-${DB_compiledrelease} ];then
  DB_pkgname="${NAMEONLY}_DEV-${PKGVERSION}-${DB_compiledrelease}"
  DB_nameonly="${NAMEONLY}_DEV"
  DB_version="${PKGVERSION}-${DB_compiledrelease}"
  DB_size="`du -k -s ${DESTDIR}/${DB_pkgname} | cut -f 1`"
  DB_fullfilename="${DB_pkgname}.pet"
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" > ${DESTDIR}/${DB_pkgname}/pet.specs
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" >> $DBFILE
  DB_dependencies="+${NAMEONLY}"
  [ -f ${DESTDIR}/${DB_pkgname}/CATEGORY ] && rm -f ${DESTDIR}/${DB_pkgname}/CATEGORY
  [ -f ${DESTDIR}/${DB_pkgname}/DEPEND ] && rm -f ${DESTDIR}/${DB_pkgname}/DEPEND
  [ -f ${DESTDIR}/${DB_pkgname}/DESCRIPTION ] && rm -f ${DESTDIR}/${DB_pkgname}/DESCRIPTION
  [ -f ${DESTDIR}/${DB_pkgname}/RDEPEND ] && rm -f ${DESTDIR}/${DB_pkgname}/RDEPEND
  [ -f ${DESTDIR}/${DB_pkgname}/SIZE ] && rm -f ${DESTDIR}/${DB_pkgname}/SIZE
  #create pet pkg...
  cd $DESTDIR
  dir2tgz ${DB_pkgname}
  tgz2pet ${DB_pkgname}.tar.gz
  cd $CURRPATH
 fi
 if [ -d ${DESTDIR}/${NAMEONLY}_NLS-${PKGVERSION}-${DB_compiledrelease} ];then
  DB_pkgname="${NAMEONLY}_NLS-${PKGVERSION}-${DB_compiledrelease}"
  DB_nameonly="${NAMEONLY}_NLS"
  DB_version="${PKGVERSION}-${DB_compiledrelease}"
  DB_size="`du -k -s ${DESTDIR}/${DB_pkgname} | cut -f 1`"
  DB_fullfilename="${DB_pkgname}.pet"
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" > ${DESTDIR}/${DB_pkgname}/pet.specs
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" >> $DBFILE
  DB_dependencies=""
  #create pet pkg...
  cd $DESTDIR
  dir2tgz ${DB_pkgname}
  tgz2pet ${DB_pkgname}.tar.gz
  cd $CURRPATH
 fi
 if [ -d ${DESTDIR}/${NAMEONLY}_DOC-${PKGVERSION}-${DB_compiledrelease} ];then
  DB_pkgname="${NAMEONLY}_DOC-${PKGVERSION}-${DB_compiledrelease}"
  DB_nameonly="${NAMEONLY}_DOC"
  DB_version="${PKGVERSION}-${DB_compiledrelease}"
  DB_size="`du -k -s ${DESTDIR}/${DB_pkgname} | cut -f 1`"
  DB_fullfilename="${DB_pkgname}.pet"
  DB_dependencies=""
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" > ${DESTDIR}/${DB_pkgname}/pet.specs
  echo "${DB_pkgname}|${DB_nameonly}|${DB_version}||${DB_category}|${DB_size}K||${DB_fullfilename}|${DB_dependencies}|${DB_description}|${DB_compileddistro}|${DB_compiledrelease}||" >> $DBFILE
  #create pet pkg...
  cd $DESTDIR
  dir2tgz ${DB_pkgname}
  tgz2pet ${DB_pkgname}.tar.gz
  cd $CURRPATH
 fi
 
 sync
done

sort $DBFILE > /tmp/$DBFILE
mv -f /tmp/$DBFILE $DBFILE
sync

###END###
