#!/bin/sh
#convert the old package database format used in Puppy 'packages.txt',
#'livepackages.txt' and 'alienpackages.txt' to the new Woof standardised format.

export LANG=C #fast.

cat ${1} | tr '\t' ' ' | tr -s ' ' | sed -e 's%\\$%%' |
while read ONELINE
do

 #old ex: "abiword-2.6.3" "abiword 2.6.3: wordprocessor" ONOFF "Document +fribidi,+gtk+,+goffice,+wv,+enchant 7556K" 
 #new: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|
 #ex: abiword-1.2.4|abiword|1.2.4|5|Document|999K|slackware/ab|abiword-1.2.4-5-i486.tgz|+aiksausus,+gtk2|a nice wordprocessor|

 #optionally on the end: compileddistro|compiledrelease|repo| (fields 11,12,13)
 #ex: slackware|12.2|official|
 #this is not normally needed, as the name of the file holding the database info, for ex
 #'Packages-slackware-12.2-official' identifies this extra information.
 
 #can't automatically separate pkgrelease numbering...
 DB_pkgname="`echo -n "$ONELINE" | cut -f 2 -d '"'`" #'geany
 DB_nameonly="`echo -n "$ONELINE" | cut -f 4 -d '"' | cut -f 1 -d ' '`" #'geany
 DB_pkgrelease=""
 DB_fullfilename="${DB_pkgname}.pet"

 DB_repo="`echo -n "$1" | rev | cut -f 1 -d '-' | rev`" #extract from filename, ex 'packages-pet-4'.
 DB_path="pet_packages-${DB_repo}"
 
 DB_version="`echo -n "$ONELINE" | cut -f 4 -d '"' | cut -f 2 -d ' ' | cut -f 1 -d ':'`" #'geany
 DB_description="`echo -n "$ONELINE" | cut -f 4 -d '"' | cut -f 3-19 -d ' '`" #'geany
 DB_category="`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 1 -d ' ' | cut -f 1 -d ':'`" #'geany
 if [ "`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 2 -d ' ' | grep '\+'`" != "" ];then #'geany
  DB_dependencies="`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 2 -d ' '`" #'geany
  DB_size="`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 3 -d ' '`" #'geany
 else
  DB_dependencies=""
  DB_size="`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 2 -d ' '`" #'geany
 fi
 
 #any extra info... ex old format:
 #"seamonkey-1.1.14-no_mailnews" "seamonkey 1.1.14-no_mailnews: browser suite" ONOFF "Internet:ubuntu:intrepid +nspr,+nss 27896K" 
 if [ "`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 1 -d ' ' | grep ':'`" != "" ];then #'geany
  DB_compileddistro="`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 1 -d ' ' | cut -f 2 -d ':'`" #'geany
  DB_compiledrelease="`echo -n "$ONELINE" | cut -f 6 -d '"' | cut -f 1 -d ' ' | cut -f 3 -d ':'`" #'geany
  echo "$DB_pkgname|$DB_nameonly|$DB_version|$DB_pkgrelease|$DB_category|$DB_size|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|$DB_compileddistro|$DB_compiledrelease|$DB_repo|"
 else
  echo "$DB_pkgname|$DB_nameonly|$DB_version|$DB_pkgrelease|$DB_category|$DB_size|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|"
 fi


done

