#!/bin/sh
#(c) Copyright 2009 Barry Kauler.
#this script is only for T2 developers, not for Woof.
#091106 updated.
#120811 db category field now supports optional subcategory: |category[:subcategory]|
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#121223 keep previous PKGS_HOMEPAGES file.
#121223 reduce deps lists to only existing t2 binary pkgs.

export LANG=C #faster.
. ./DISTRO_SPECS
[ ! "$DISTRO_DB_SUBNAME" ] && DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #121102 fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.
. ./DISTRO_COMPAT_REPOS-t2-${DISTRO_COMPAT_VERSION} #has PKG_DOCS_DISTRO_COMPAT
. ./DISTRO_PKGS_SPECS-t2-${DISTRO_COMPAT_VERSION}

FIND_CAT="./support/find_cat"
ONE_PKGLISTS_COMPAT="`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|' | head -n 1`" #see file DISTRO_PKGS_SPECS-t2
#...ex: Packages-t2-${DISTRO_COMPAT_VERSION}-official, which is Packages-t2-puppy5-official
ONE_PKGREPO_COMPAT="`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 2 -d '|' | head -n 1`" #see file DISTRO_PKGS_SPECS-t2

#db format:
#pkgname|nameonly|version|pkgrelease|category[;subcategory]|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.


echo "This script is for a developer who has compiled with T2."
echo "The T2 build system is local, with compiled binary packages."
echo "This script will read from T2 and create a Puppy standard"
echo "package database. Normally this is then uploaded to the Internet,"
echo "to a location specified by the PKG_DOCS_DISTRO_COMPAT variable"
echo "in file DISTRO_COMPAT_REPOS-t2-${DISTRO_COMPAT_VERSION}."

echo
echo "Please enter the full path to the T2 build system"
echo -n "(ex: /mnt/sdb3/t2-trunk): "
read T2PATH
if [ "$T2PATH" = "" ];then
 [ -f /tmp/0pre-t2path ] && T2PATH="`cat /tmp/0pre-t2path`"
fi
echo -n $T2PATH > /tmp/0pre-t2path

[ ! -d $T2PATH ] && exit
[ ! -d $T2PATH/build ] && exit

#get the project directory in the 'build' subdirectory...
echo
echo -n "Enter the name of the directory inside 'build' subdirectory: "
read T2PROFILE
if [ "$T2PROFILE" = "" ];then
 [ -f /tmp/0pre-t2profile ] && T2PROFILE="`cat /tmp/0pre-t2profile`"
fi
echo -n $T2PROFILE > /tmp/0pre-t2profile

[ ! -d $T2PATH/build/$T2PROFILE ] && exit

#121223 get the names of all the pkgs, use below to reduce deps lists...
ALLPKGS="$(find ${T2PATH}/build/${T2PROFILE}/var/adm/descs -mindepth 1 -maxdepth 1 -type f | rev | cut -f 1 -d '/' | rev | grep -v -E '^00-dirtree|^linux')"
echo "$ALLPKGS" > /tmp/woof-0pre-allpkgs

#what we want is in $T2PATH/build/$T2PROFILE/var/adm
DB_pkgrelease=""
echo -n "" > $ONE_PKGLISTS_COMPAT
T2DESCS="`find $T2PATH/build/$T2PROFILE/var/adm/descs -maxdepth 1 -type f`"
for ONEDESC in $T2DESCS
do
 DB_nameonly="`basename $ONEDESC`"
 [ "$DB_nameonly" = "00-dirtree" ] && continue
 [ "$DB_nameonly" = "linux" ] && continue #121223
 echo -n "$DB_nameonly "
 DESC="`cat $ONEDESC | tr '\t' ' ' | tr -s ' '`"
 DB_version="`echo "$DESC" | grep '^\[V\]' | head -n 1 | cut -f 2 -d ' '`"
 DB_pkgname="${DB_nameonly}-${DB_version}"
 DB_description="`echo "$DESC" | grep '^\[I\]' | head -n 1 | cut -f 2-19 -d ' ' | sed -e 's/[^0-9a-zA-Z.+_= ]//g'`"
 DB_path="${DISTRO_COMPAT_VERSION}" #puppy5
 DB_fullfilename="${DB_pkgname}.tar.bz2"
 
 DB_size=""
 if [ -f $T2PATH/build/$T2PROFILE/var/adm/cache/$DB_nameonly ];then
  CACHE="`cat $T2PATH/build/$T2PROFILE/var/adm/cache/$DB_nameonly | tr '\t' ' ' | tr -s ' '`"
  SIZEM="`echo "$CACHE" | grep '^\[SIZE\]' | head -n 1 | cut -f 2 -d ' '`"
  SIZEK=`dc $SIZEM 1024 mul p | cut -f 1 -d '.'`
  DB_size="$SIZEK"'K'
 fi
 
 #121223...
 DEPSALL="`cat $T2PATH/build/$T2PROFILE/var/adm/dependencies/$DB_nameonly | grep "^${DB_nameonly}:" | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`"
 echo "$DEPSALL" > /tmp/woof-0pre-depsall
 DEPS="$(grep -x -f /tmp/woof-0pre-allpkgs /tmp/woof-0pre-depsall)"
 DB_dependencies="`echo "$DEPS" | tr '\n' ' ' | tr -s ' ' | sed -e 's% $%%' -e 's% %,+%g' -e 's%^%+%'`"
 
 xDB_description="$DB_description"
 [ "$DB_description" = "" ] && xDB_description="nothing"
 DB_category="`${FIND_CAT} $DB_nameonly "$xDB_description"`" #120812 see updated find_cat
 
 echo "$DB_pkgname|$DB_nameonly|$DB_version|$DB_pkgrelease|$DB_category|$DB_size|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|$DISTRO_BINARY_COMPAT|$DISTRO_COMPAT_VERSION||" >> $ONE_PKGLISTS_COMPAT
 
done
echo

#sort...
sort --key=1 --field-separator='|' $ONE_PKGLISTS_COMPAT > /tmp/0pre-$ONE_PKGLISTS_COMPAT
mv -f /tmp/0pre-$ONE_PKGLISTS_COMPAT $ONE_PKGLISTS_COMPAT

##################
#now create a db of pkgs homepages, file PKGS_HOMEPAGES.
#note, file also used in /usr/sbin/indexgen.sh and /usr/local/petget/fetchinfo.sh

HOMEPAGEDB='abiword http://www.abisource.com/
alsa http://www.alsa-project.org/main/index.php/Main_Page
asunder http://littlesvr.ca/asunder/
axel http://wilmer.gaast.net/main.php/axel.html
ayttm http://ayttm.sourceforge.net/
bluefish http://bluefish.openoffice.nl/
chmsee http://chmsee.gro.clinux.org/
chtheme http://plasmasturm.org/code//gtk-chtheme/
cups http://www.cups.org/
dkop http://kornelix.squarespace.com/dkop/
e3 http://www.sax.de/~adlibit
fotox http://kornelix.squarespace.com/fotox/
fotoxx http://kornelix.squarespace.com/fotoxx/
hardinfo http://hardinfo.berlios.de/HomePage
gcolor2 http://gcolor2.sourceforge.net/
gcurl http://www.gnomefiles.org/app.php/gCurl
gexec http://www.cs.berkeley.edu/~bnc/gexec/
gftp http://www.gftp.org/
glipper http://glipper.sourceforge.net/
gnomeppp http://www.gnomefiles.org/app.php?soft_id=41
gnome-ppp http://www.gnomefiles.org/app.php?soft_id=41
gnumeric http://www.gnome.org/projects/gnumeric/
gparted http://gparted.sourceforge.net/
grub http://www.gnu.org/software/grub/
gtkam http://sourceforge.net/projects/gphoto
hardinfo http://hardinfo.berlios.de/
isomaster http://littlesvr.ca/isomaster/
leafpad http://tarot.freeshell.org/leafpad/
mhwaveedit https://gna.org/projects/mhwaveedit/
mtr http://www.BitWizard.nl/mtr
notecase http://notecase.sourceforge.net/
picpuz http://kornelix.squarespace.com/picpuz/
python http://www.python.org/
seamonkey http://www.seamonkey-project.org/
sylpheed http://sylpheed.good-day.net/
sylpheed_portable http://bkhome.org/blog/?viewDetailed=02735
sysprof http://www.daimi.au.dk/~sandmann/sysprof/
tea http://tea-editor.sourceforge.net/
valaide http://www.valaide.org/
valide http://www.valaide.org/
xarchive http://xarchive.sourceforge.net/
xsane http://www.xsane.org/
xsoldier http://www.interq.or.jp/libra/oohara/xsoldier/
zmixer http://zwin.org/projects/zmixer/'

#echo "$HOMEPAGEDB" > PKGS_HOMEPAGES #something to start with. 121223 removed.

echo
echo "$ONE_PKGLISTS_COMPAT has been created."
echo "This needs to be uploaded to: "
echo "  $ONE_PKGREPO_COMPAT"
echo "It will also be used locally by the Woof build scripts."
echo "If you run '0setup', it will see local $ONE_PKGLISTS_COMPAT"
echo "and will not download it."
echo "Good to run '0setup' though as it checks validity of some things."

echo
echo -n "Press ENTER to continue: "
read yesgoon

echo
echo "Now building file PKGS_HOMEPAGES ..."
#w480 get full db, will filter later...
ALLDESCS="`find $T2PATH/package -type f -name \*.desc`"
##no, narrow it down to only those actually compiled...
#ALLDESCS="$T2DESCS"
for ONEDESC in $ALLDESCS
do
 NAMEONLY="`basename $ONEDESC .desc`"
 echo -n "$NAMEONLY "
 HOMEPAGE="`cat $ONEDESC | grep '^\[U\]' | head -n 1 | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`"
 [ "$HOMEPAGE" != "" ] && echo "$NAMEONLY $HOMEPAGE" >> PKGS_HOMEPAGES
done
echo

#sort... 121223 fix key...
sort --unique --key=1,1 --field-separator=' ' PKGS_HOMEPAGES > /tmp/0pre-PKGS_HOMEPAGES
mv -f /tmp/0pre-PKGS_HOMEPAGES PKGS_HOMEPAGES

echo
echo "The raw binary packages have to be copied from T2 into 'packages-bz2-${DISTRO_COMPAT_VERSION}'."
echo -n "Press ENTER to do this: "
read yesdoit
if [ "$yesdoit" = "" ];then
 mkdir -p packages-bz2-${DISTRO_COMPAT_VERSION}
 cp -a -f $T2PATH/build/$T2PROFILE/TOOLCHAIN/pkgs/* packages-bz2-${DISTRO_COMPAT_VERSION}/
 sync
 echo "...done"
fi
rm -f packages-bz2-${DISTRO_COMPAT_VERSION}/00-dirtree*.bz2 #121223
rm -f packages-bz2-${DISTRO_COMPAT_VERSION}/linux-[23]*.bz2 #121223

###END###
