#!/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).
#'createtbz' converts those binary pkgs to tar.bz2 and creates a db entry.

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

WOOF_HOSTARCH='x86'
WOOF_TARGETARCH='arm'
NLS='_NLS'
EXE=''
DEV='_DEV'
DOC='_DOC'
DB_compileddistro='gentoo'
DB_compiledrelease='gap6' #"Gentoo 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="packages-gentoo-gap6" #where the tar.bz2 pkgs are to be created.
[ -d $DESTDIR ] && rm -rf packages-gentoo-gap6/*

#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
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} ] && destAPKGNAME="${destAPKGNAME}-SECOND" #precaution.
 [ -d ${DESTDIR}/${destAPKGNAME}-SECOND ] && 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 ${DESTDIR}/${destAPKGNAME}/usr/share/applications ${DESTDIR}/${destAPKGNAME}/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 "${DESTDIR}/${destAPKGNAME}${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 ${DESTDIR}/${destAPKGNAME}/usr/share/pixmaps ] && FNDICON="`find ${DESTDIR}/${destAPKGNAME}/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 ${DESTDIR}/${destAPKGNAME}/usr/local/lib/X11/mini-icons ] && FNDICON="`find ${DESTDIR}/${destAPKGNAME}/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...
    xPTN="s%${DESTDIR}/${destAPKGNAME}%%"
    FNDICON="`find ${DESTDIR}/${destAPKGNAME} /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 "${xPTN}" | grep -i -E 'png$|xpm$|jpg$|jpeg$|gif$|svg$' | head -n 1`"
    if [ "$FNDICON" ];then
     ICONNAMEONLY="`basename "$FNDICON"`"
     mkdir -p ${DESTDIR}/${destAPKGNAME}/usr/share/pixmaps
     ln -snf "$FNDICON" ${DESTDIR}/${destAPKGNAME}/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
 
 #for each created dir, create 'pet.specs' db entry...
 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%$%,%' | sed -e '/^+[^a-zA-Z]/d' | 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}/${destAPKGNAME} ];then
  DB_pkgname="${destAPKGNAME}"
  DB_nameonly="${NAMEONLY}"
  DB_version="${PKGVERSION}"
  DB_size="`du -k -s ${DESTDIR}/${DB_pkgname} | cut -f 1`"
  DB_fullfilename="${DB_pkgname}.tar.bz2"
  #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
 #fi
 sync

 #create tarball...
 cd ${DESTDIR}
 [ ! -d ${destAPKGNAME} ] && exit #sanity check.
 cd ${destAPKGNAME}
 tar -c -f ../${destAPKGNAME}.tar *
 cd ..
 [ ! -f ${destAPKGNAME}.tar ] && exit #sanity check.
 bzip2 ${destAPKGNAME}.tar
 sync
 rm -rf ${destAPKGNAME}
 cd ..
 
 sleep 1
done

sort $DBFILE > /tmp/$DBFILE
mv -f /tmp/$DBFILE $DBFILE
sync
echo "...finished"

###END###
