#!/bin/sh
#Barry Kauler, Oct 2010
#3builddistro to call this.
#there is a problem with packages, say abiword, abiword_DEV, abiword_DOC, abiword_NLS
#maybe each being in a different category. see info:
#puppy package database format:
#pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
#...'compileddistro|compiledrelease' (fields 11,12) identify where the package was compiled.
#in the PPM it is better if they are grouped in same category.
#120812 db category now allows optional subcategory (for which an icons exists in /usr/local/lib/X11/mini-icons).
#ie: pkgname|nameonly|version|pkgrelease|category[;category]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|

#ex: before:
#homebank-4.3-w5|homebank|4.3-w5||Business|604K||homebank-4.3-w5.pet|+gtk+|HomeBank finance management|puppy|wary5||
#homebank_DOC-4.3-w5|homebank_DOC|4.3-w5||BuildingBlock|1892K||homebank_DOC-4.3-w5.pet||personal finance||||
#homebank_NLS-4.3-w5|homebank_NLS|4.3-w5||BuildingBlock|1972K||homebank_NLS-4.3-w5.pet|+homebank|personal finance||||
#after:
#homebank-4.3-w5|homebank|4.3-w5||Business|604K||homebank-4.3-w5.pet|+gtk+|HomeBank finance management|puppy|wary5||
#homebank_DOC-4.3-w5|homebank_DOC|4.3-w5||Business|1892K||homebank_DOC-4.3-w5.pet||personal finance||||
#homebank_NLS-4.3-w5|homebank_NLS|4.3-w5||Business|1972K||homebank_NLS-4.3-w5.pet|+homebank|personal finance||||

[ ! $1 ] && exit 1
DBFILE="$1" #ex: Packages-puppy-wary5-official
[ ! -f $DBFILE ] && exit 1
echo -n '' > /tmp/rationalized_categories

echo "Rationalizing categories in ${DBFILE}, please wait..."

cat $DBFILE |
while read ONEDBENTRY
do
 ENTRYfixed="$ONEDBENTRY"
 DB_nameonly="`echo -n "$ONEDBENTRY" | cut -f 2 -d '|'`"
 if [ "`echo -n "$DB_nameonly" | grep -E '_DEV|_DOC|_NLS'`" = "" ];then
  DB_category="`echo -n "$ONEDBENTRY" | cut -f 5 -d '|' | cut -f 1 -d ';'`" #120812
  nameonlyref="$DB_nameonly"
 else
  #big assumption that these are listed in db afterward...
  nameonlybase="`echo -n "$DB_nameonly" | rev | cut -c 5-999 | rev`" #remove the '_DEV' part.
  #echo "nameonlyref=$nameonlyref nameonlybase=$nameonlybase" #TEST
  if [ "$nameonlybase" = "$nameonlyref" ];then
   ENTRYfixed="`echo -n "$ONEDBENTRY" | tr '|' '\n' | head -n 4 | tr '\n' '|'`"
   ENTRYfixed="${ENTRYfixed}${DB_category}|"
   ENTRYfixed="${ENTRYfixed}`echo -n "$ONEDBENTRY" | tr '|' '\n' | tail -n +6 | tr '\n' '|'`"
  fi
 fi
 echo "$ENTRYfixed" >> /tmp/rationalized_categories
done
exit 0

###END###
