#!/bin/ash
#(c) Copyright 2009 Barry Kauler.
#100617 support slackware .txz pkgs.
#100626 '-' chars were getting removed from dependencies.

export LANG=C #faster.

if [ -f ./DISTRO_SPECS ];then
. ./DISTRO_SPECS
if [ -f ./DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #v431
. ./DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
fi
if [ -f ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #w478
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
else
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}
fi
. ./PKGS_MANAGEMENT
. ./DISTRO_PET_REPOS
 RUNNINGPUP='no'
else
 #want to run this script in running puppy, to update db's...
. /etc/DISTRO_SPECS
. /root/.packages/DISTRO_COMPAT_REPOS #v431
. /root/.packages/DISTRO_PKGS_SPECS
. /root/.packages/PKGS_MANAGEMENT
. /root/.packages/DISTRO_PET_REPOS
 cd /root/.packages
 RUNNINGPUP='yes'
fi

#remove comments from PKGS_SPECS_TABLE
PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep -v '^#'`"

#w480 check puppy pkg db files...
if [ "$RUNNINGPUP" = "no" ];then
 ./support/fix-puppy-dbs
 [ $? -eq 1 ] && exit
fi

#w091017
if [ -f ./support/ppkg ];then
 PPKG="./support/ppkg"
 CTG="./support/ppkg.ctg"
else
 PPKG="/usr/local/petget/ppkg" #0setup also used in running puppy.
 CTG="/usr/local/petget/ppkg.ctg" # "
fi

#w015 to speed things up, this func replaced by compiled app support/find_cat ...
if [ -f ./support/find_cat ];then
 FIND_CAT="./support/find_cat"
else
 FIND_CAT="/usr/local/petget/find_cat" #0setup also used in running puppy.
fi
#find_cat_func() {
# DB_category=""
#    noPATTERN=" $DB_nameonly "
#    [ "`echo "$PKG_CAT_Desktop" | grep "$noPATTERN"`" != "" ] && DB_category="Desktop"
#    [ "`echo "$PKG_CAT_System" | grep "$noPATTERN"`" != "" ] && DB_category="System"
#    [ "`echo "$PKG_CAT_Utility" | grep "$noPATTERN"`" != "" ] && DB_category="Utility"
#    [ "`echo "$PKG_CAT_Filesystem" | grep "$noPATTERN"`" != "" ] && DB_category="Filesystem"
#    [ "`echo "$PKG_CAT_Graphic" | grep "$noPATTERN"`" != "" ] && DB_category="Graphic"
#    [ "`echo "$PKG_CAT_Document" | grep "$noPATTERN"`" != "" ] && DB_category="Document"
#    [ "`echo "$PKG_CAT_Calculate" | grep "$noPATTERN"`" != "" ] && DB_category="Calculate"
#    [ "`echo "$PKG_CAT_Personal" | grep "$noPATTERN"`" != "" ] && DB_category="Personal"
#    [ "`echo "$PKG_CAT_Network" | grep "$noPATTERN"`" != "" ] && DB_category="Network"
#    [ "`echo "$PKG_CAT_Internet" | grep "$noPATTERN"`" != "" ] && DB_category="Internet"
#    [ "`echo "$PKG_CAT_Multimedia" | grep "$noPATTERN"`" != "" ] && DB_category="Multimedia"
#    [ "`echo "$PKG_CAT_Fun" | grep "$noPATTERN"`" != "" ] && DB_category="Fun"
#    [ "`echo "$PKG_CAT_Develop" | grep "$noPATTERN"`" != "" ] && DB_category="Develop"
#    [ "`echo "$PKG_CAT_BuildingBlock" | grep "$noPATTERN"`" != "" ] && DB_category="BuildingBlock"
#    [ "`echo "$PKG_CAT_Help" | grep "$noPATTERN"`" != "" ] && DB_category="Help"
# #attempt to find it from the description...
# if [ "$DB_category" = "" -a "$DB_description" != "" ];then
#  #need to have these in a particular order...
#  [ "`echo -n " $DB_description " | grep -i -E ' system | print | printing | process | hardware | monitor'`" != "" ] && DB_category="System"
#  [ "`echo -n " $DB_description " | grep -i -E ' utility | archive| remote | backup | partition | terminal | storage | console '`" != "" ] && DB_category="Utility"
#  [ "`echo -n " $DB_description " | grep -i -E ' window | screen | icon| desktop '`" != "" ] && DB_category="Desktop"
#  [ "`echo -n " $DB_description " | grep -i -E ' filesystem | disk | batch | catalog| file manager | renamer | file | drive| disk| disc '`" != "" ] && DB_category="Filesystem"
#  [ "`echo -n " $DB_description " | grep -i -E ' document | wordprocessor | text editor | hex editor | html editor |pdf editor |pdf viewer | dictionary | wysiwyg | open office| openoffice | postscript viewer | presentation | desktop publishing | word processor'`" != "" ] && DB_category="Document"
#  [ "`echo -n " $DB_description " | grep -i -E ' calculate | calculator | spreadsheet | financ| measure| accounting | bookkeeping '`" != "" ] && DB_category="Calculate"
#  [ "`echo -n " $DB_description " | grep -i -E ' personal | wiki | password | wikki | wik| notes | blog | address| database '`" != "" ] && DB_category="Personal"
#  [ "`echo -n " $DB_description " | grep -i -E ' network | modem | dialup | firewall | file sharing | samba | server | port scanner | wireless | bluetooth | irda | lan | wan | vpn '`" != "" ] && DB_category="Network"
#  [ "`echo -n " $DB_description " | grep -i -E ' internet | download| chat | irc |icq | yahoo |msn | instant messag| im client | ftp | sftp | ssh | secure shell | torrent | podcast | mail | email | news | browser | web | telnet '`" != "" ] && DB_category="Internet"
#  [ "`echo -n " $DB_description " | grep -i -E ' multimedia | scanner | audio play| sound play| audio edit| sound edit| video play| video edit| video view| media player | song | ripper | recorder | burner | mixer '`" != "" ] && DB_category="Multimedia"
#  [ "`echo -n " $DB_description " | grep -i -E ' game | games | boardgame | fun '`" != "" ] && DB_category="Fun"
#  [ "`echo -n " $DB_description " | grep -i -E ' paint | painting | image edit| bitmap edit| vector edit| photo| graphic| font edit| image view| thumbnail'`" != "" ] && DB_category="Graphic"
#  [ "`echo -n " $DB_description " | grep -i -E ' develop | development | programming | language | lua | vala |lisp | mono | assembler | bash '`" != "" ] && DB_category="Develop"
#  [ "`echo -n " $DB_description " | grep -i -E ' howto | documentation | faq | doc | docs | help '`" != "" ] && DB_category="Help"
#  [ "`echo -n " $DB_description " | grep -i " library "`" != "" ] && DB_category="BuildingBlock"
#  [ "`echo -n "$DB_nameonly" | grep "^lib"`" != "" ] && DB_category="BuildingBlock"
# fi
# [ "$DB_category" = "" ] && DB_category="BuildingBlock" #default.
#}

#w091018 now logging errors for future recall...
mkdir -p /var/woof
RUNDATE="`date`"
echo -n "" > /var/woof/0setup_fail_report_$RUNNINGPUP #RUNNINGPUP=yes or no. latter if woof.
echo "This is a report on the last time the '0setup' script was run.
Date and time '0setup' last run: ${RUNDATE}
Compatible-distro and release-name: ${DISTRO_BINARY_COMPAT}, ${DISTRO_COMPAT_VERSION}
Mostly only errors get logged, so the less seen below, the better.
Log of last run of '0setup':
" >> /var/woof/0setup_fail_report_$RUNNINGPUP

#download docs on compatible-distro pkgs...
echo -n "" > /tmp/arch_more_fields_pre
for PKGLISTSPEC in $PKG_DOCS_DISTRO_COMPAT  #see file DISTRO_PKGS_SPECS-ubuntu
do
 PKGLISTFILE="`echo -n "$PKGLISTSPEC" | cut -f 3 -d '|'`"
 PKGLISTURI="`echo -n "$PKGLISTSPEC" | cut -f 2 -d '|'`"
 [ "$PKGLISTURI" = "" ] && continue #w478 no url, pkg db file only kept in woof.
 skipdl=""
 if [ "$RUNNINGPUP" = "no" -a -f $PKGLISTFILE ];then
  echo
  echo "Local db file '${PKGLISTFILE}' already exists."
  echo "Press ENTER key only to upgrade it,"
  echo -n "or any other printable char to skip: "
  read skipdl
 fi
 if [ "$skipdl" = "" ];then
  echo
  echo "A package information database file needs to be downloaded."
  echo "This will be downloaded from:"
  echo "$PKGLISTURI"
  echo "and will be processed and named $PKGLISTFILE."
  echo -n "Press ENTER key to download, any other to abort: "
  read downloadit
  [ "$downloadit" != "" ] && exit
  DLFILE="`basename $PKGLISTURI`"
  [ -f $DLFILE ] && mv -f $DLFILE /tmp/${DLFILE}-backup1 #v431 otherwise wget creates a new file ${DLFILE}.1
  rxvt -name pet -bg orange -geometry 80x10 -e wget $PKGLISTURI
  sync
  xDLFILE="$DLFILE"
  if [ -f $DLFILE ];then
   case ${DISTRO_BINARY_COMPAT} in
    ubuntu|debian)
     xDLFILE="`basename $DLFILE .bz2`"
     bunzip2 $DLFILE
     RETSTAT=$?
     [ $RETSTAT -eq 0 ] && mv -f $xDLFILE ${PKGLISTFILE}pre
    ;;
    slackware)
     RETSTAT=0
     mv -f $xDLFILE ${PKGLISTFILE}pre
    ;;
    arch)
     #have to do heavy pre-pre-processing to create one compact database file...
     echo "Processing $DLFILE please wait..."
     xDLFILE="`basename $DLFILE .gz`" #actually it's a tarball: core.abs.tar.gz, extra.abs.tar.gz, community.abs.tar.gz
     gunzip $DLFILE
     RETSTAT=$?
     if [ $RETSTAT -eq 0 ];then
      rm -rf sandbox0 2>/dev/null
      mkdir sandbox0
      mv -f $xDLFILE sandbox0/
      cd sandbox0
      tar -x -f $xDLFILE
      xxDLFILE="`basename $xDLFILE .abs.tar`" #this will be core, extra, community
      echo -n "" > ../${PKGLISTFILE}pre
      for ONEPKGBUILD in `find ${xxDLFILE} -type f -name PKGBUILD | tr '\n' ' '`
      do
       #w016 some pkg maintainers use '\' for line continuation, messes up this script...
       rm -f /tmp/PKGBUILDfixed 2>/dev/null
       cat ${ONEPKGBUILD} |
       while read ONELINEfixed
       do
        echo "$ONELINEfixed" >> /tmp/PKGBUILDfixed
       done
       mv -f /tmp/PKGBUILDfixed ${ONEPKGBUILD}
       #'depends=' field can have weird stuff like this '$(basename /lib/klibc-*.so .so)', get rid: sed -e 's%\$\([^)]*\)%%'
       EXTRACTEDINFO="`cat ${ONEPKGBUILD} | grep -E '^pkgname=|^pkgver=|^pkgrel=|^depends=|^pkgdesc=' | tr '\t' ' ' | tr '|' ' ' | tr '\-' '|' | sed -e 's%\$\([^)]*\)%%' | sed -e 's/[^0-9a-zA-Z.+_|= ]//g' | tr '|' '-' | sed -e 's/^pkgname=/pkgname="/' | sed -e 's/^pkgver=/pkgver="/' | sed -e 's/^pkgrel=/pkgrel="/' | sed -e 's/^depends=/depends="/' | sed -e 's/^pkgdesc=/pkgdesc="/' | sed -e 's/$/"/' | tr '\n' ' ' | tr -s ' '`"
       echo "$EXTRACTEDINFO" >> ../${PKGLISTFILE}pre
      done
      cd ..
      rm -rf sandbox0 2>/dev/null
      #w016 bugfix remove lines that failed to get a pkgname (only one, amarok-engine-xine)...
      sort --field-separator='"' --key=2 ${PKGLISTFILE}pre | grep -v 'pkgname="" ' > /tmp/$PKGLISTFILE
      sync
      mv -f /tmp/$PKGLISTFILE ${PKGLISTFILE}pre
      
      #now for a hack, need some supplementary info...
      mkdir sandbox0
      zPKGLISTURI="`echo -n "$PKGLISTURI" | sed -e 's%\.abs\.%.db.%'`"
      zDLFILE="`basename $zPKGLISTURI`" #MiBu bugfix.
      echo "Downloading supplementary file $zDLFILE ..."
      mv -f $zDLFILE /tmp/${zDLFILE}-backup1 #v431 otherwise wget creates a new file ${zDLFILE}.1
      rxvt -name pet -bg orange -geometry 80x10 -e wget $zPKGLISTURI
      sync
      #also downloading .db.tar.gz files, as these have size info missing in the .abs.tar.gz...
      zzDLFILE="`basename $zDLFILE .db.tar`" #this will be core, extra, community
      #tarball does not expand into a dir, so create one...
      mkdir -p sandbox0/$zzDLFILE
      mv -f $zDLFILE sandbox0/$zzDLFILE/
      cd sandbox0
      cd $zzDLFILE
      tar -x -f $zDLFILE
      cd ..
      for ONEPKGBUILD in `find ${zzDLFILE} -type f -name desc | tr '\n' ' '`
      do
       SIZE_BYTES=`cat $ONEPKGBUILD | tr '\n' ' ' | tr '\t' ' ' | tr -s ' ' | grep -o '%ISIZE%.*' | cut -f 2 -d ' '`
       SIZE_K=`expr $SIZE_BYTES \/ 1024`
       FULLPKGNAME="`cat $ONEPKGBUILD | tr '\n' ' ' | tr '\t' ' ' | tr -s ' ' | grep -o '%FILENAME%.*' | cut -f 2 -d ' '`"
       #log this for later processing...
       echo "$FULLPKGNAME $SIZE_K" >> /tmp/arch_more_fields_pre
      done
      cd ..
      rm -rf sandbox0 2>/dev/null
      
     fi
    ;;
    t2) #w017
     RETSTAT=0
    ;;
    puppy)
     RETSTAT=0
    ;;
   esac
   if [ $RETSTAT -eq 0 ];then
    echo "...success."
   else
    echo "Downloaded file is corrupted. Deleting file and aborting."
    rm -f $xDLFILE 2>/dev/null
    rm -f $DLFILE 2>/dev/null
    exit
   fi
  else
   echo "Failed to download, aborting."
   echo "
Failed to download db file: 
 $PKGLISTURI
...exited from 0setup script." >> /var/woof/0setup_fail_report_$RUNNINGPUP
   exit
  fi
 fi
done

###download pet pkg databases...
for PKGLISTSPEC in $PKG_DOCS_PET_REPOS  #see file DISTRO_PET_REPOS
do
 PKGLISTFILE="`echo -n "$PKGLISTSPEC" | cut -f 3 -d '|'`"
 PKGLISTURI="`echo -n "$PKGLISTSPEC" | cut -f 2 -d '|'`"
 if [ "$RUNNINGPUP" = "yes" -o ! -f $PKGLISTFILE ];then
  echo
  echo "A package information database file needs to be downloaded."
  echo "This will be downloaded from:"
  echo "$PKGLISTURI"
  echo "and will be named $PKGLISTFILE."
  echo -n "Press ENTER key to download, any other to abort: "
  read downloadit
  [ "$downloadit" != "" ] && exit
  DLFILE="`basename $PKGLISTURI`"
  mv -f $DLFILE /tmp/${DLFILE}-backup1 #v431 otherwise wget creates a new file ${DLFILE}.1
  rxvt -name pet -bg orange -geometry 80x10 -e wget $PKGLISTURI
  sync
  [ -f $DLFILE ] && echo "...success"
 fi
done

v1PATTERN="s%\\-[0-9.]*${DISTRO_BINARY_COMPAT}[0-9.]*$%%"
v2PATTERN="s%\\+[0-9.]*${DISTRO_BINARY_COMPAT}[0-9.]*$%%"

#w008 convert pkg database ${PKGLISTFILE}pre to a standard format...
#each line: 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|
PKGLISTS_COMPAT="`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|' | tr '\n' ' '`" #see file DISTRO_PKGS_SPECS-ubuntu
#...format ex: 'Packages-ubuntu-intrepid-main Packages-ubuntu-intrepid-universe'
for ONE_PKGLISTS_COMPAT in $PKGLISTS_COMPAT
do
 [ ! -f ${ONE_PKGLISTS_COMPAT}pre ] && continue
 echo
 echo "Processing $ONE_PKGLISTS_COMPAT into a standard format..."
 echo -n "" > $ONE_PKGLISTS_COMPAT
 case ${DISTRO_BINARY_COMPAT} in
  ubuntu|debian)
   #REPODATA="`cat ${ONE_PKGLISTS_COMPAT}pre | grep -E '^Package:|^Installed-Size:|^Architecture:|^Version:|^Depends:|^Filename:|^Description:' | tr '\t' ' ' | tr -s ' ' | sed -e 's% $%%' | tr '\n' ' ' | sed -e 's% Package: %\nPackage: %g'`"
   ##...one line ex: Package: abiword Installed-Size: 7604 Architecture: i386 Version: 2.6.4-4ubuntu4 Depends: .... Filename: pool/main/a/abiword/abiword_2.6.4-4ubuntu4_i386.deb Description: efficient, featureful word processor with collaboration
   #echo "$REPODATA" |
   #while read DB_ONELINE
   #do
   # [ "$DB_ONELINE" = "" ] && continue #w016 precaution.
   # DB_nameonly="`echo -n "$DB_ONELINE" | cut -f 2 -d ' '`"
   # [ "`echo -n "$DB_nameonly" | grep '\\-dbg$'`" != "" ] && continue #screen out debug pkgs.
   # DB_size="`echo -n "$DB_ONELINE" | grep -o 'Installed-Size: .*' | cut -f 2 -d ' '`"'K'
   # #DB_version="`echo -n "$DB_ONELINE" | grep -o 'Version: .*' | cut -f 2 -d ' ' | cut -f 1 -d '-'`"
   # DB_fullfilename="`echo -n "$DB_ONELINE" | grep -o 'Filename: .*' | cut -f 2 -d ' ' | rev | cut -f 1 -d '/' | rev`"
   # DB_path="`echo -n "$DB_ONELINE" | grep -o 'Filename: .*' | cut -f 2 -d ' ' | rev | cut -f 2-9 -d '/' | rev`"
   # DB_version="`echo -n "$DB_fullfilename" | cut -f 2 -d '_' | rev | cut -f 2-5 -d '-' | rev`"
   # DB_pkgrelease="`echo -n "$DB_fullfilename" | cut -f 2 -d '_' | rev | cut -f 1 -d '-' | rev`"
   # DB_description="`echo -n "$DB_ONELINE" | grep -o 'Description: .*' | cut -f 2-19 -d ' ' | sed -e 's/[^0-9a-zA-Z.+_= ]//g'`"
   # #w015 find_cat_func replaced by compiled app...
   # xDB_description="$DB_description"
   # [ "$DB_description" = "" ] && xDB_description="nothing"
   # DB_category="`${FIND_CAT} $DB_nameonly "$xDB_description"`"
   # DB_dependencies="`echo -n "$DB_ONELINE" | grep -o 'Depends: .* Filename: ' | sed -e 's% Filename: %%' | cut -f 2-99 -d ' ' | sed -e 's%, %\n%g' | cut -f 1 -d ' ' | tr '\n' ' ' | sed -e 's% $%%' | sed -e 's% %,+%g' | sed -e 's%,$%%'`"
   # [ "$DB_dependencies" != "" ] && DB_dependencies='+'"$DB_dependencies"
   # DB_pkgname="${DB_nameonly}_${DB_version}"
   # echo "$DB_pkgname|$DB_nameonly|$DB_version|$DB_pkgrelease|$DB_category|$DB_size|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|" >> $ONE_PKGLISTS_COMPAT
   # echo -n "$DB_nameonly "
   #done
   #w091017: Wosh has written a super-fast converter...
   #${PPKG} -c ${DISTRO_BINARY_COMPAT} -x debug -i ${CTG} ${ONE_PKGLISTS_COMPAT}pre ${ONE_PKGLISTS_COMPAT}
   #chmod 644 $ONE_PKGLISTS_COMPAT
   #w091021 ...but has some problems. for now, use this...

   while read DB_ONELINE
   do
    eval $DB_ONELINE
    case $DB_ONELINE in
     Description*)
      DB_fullfilename="`echo -n "$Filename" | rev | cut -f 1 -d '/' | rev`"
      DB_path="`echo -n "$Filename" | rev | cut -f 2-9 -d '/' | rev`"
      DB_version="`echo -n "$Version" | sed -e 's%^[0-9]:%%' -e "$v1PATTERN" -e "$v2PATTERN"`"
      DB_pkgrelease="`echo -n "$Version" | sed -e 's%^[0-9]:%%' -e "s%${DB_version}%%" -e 's%^\\-%%' -e 's%^\\+%%'`"
      DB_pkgname="${Package}_${DB_version}"
      DB_category="`${FIND_CAT} $Package "$Description"`"
      DB_dependencies="`echo -n "$Depends" | sed -e 's%, %\n%g' | cut -f 1 -d ' ' | tr '\n' ' ' | sed -e 's% $%%' -e 's% %,+%g' -e 's%,$%%'`"
      [ "$DB_dependencies" != "" ] && DB_dependencies='+'"$DB_dependencies"
      echo "$DB_pkgname|$Package|$DB_version|$DB_pkgrelease|$DB_category|${InstalledSize}K|$DB_path|$DB_fullfilename|$DB_dependencies|$Description|" >> $ONE_PKGLISTS_COMPAT
      echo -n "$Package "
     ;;
    esac
   done<<_END1
$(grep -E '^Package:|^Installed\-Size:|^Architecture:|^Version:|^Depends:|^Filename:|^Description:' ${ONE_PKGLISTS_COMPAT}pre | tr '[\t"|`]' ' ' | tr -s ' ' | grep -v '\\-dbg$' | sed -e 's%^Installed\-Size%InstalledSize%' -e 's%: %="%' -e 's% $%%' -e 's%$%"%')
_END1
   
  ;;
  slackware)
   #note stabellini site has PACKAGES.TXT for the official 12.2 repo, with dependency info.
   #however, there is an extra 'PACKAGE MIRROR' field so i have added this preprocess line...
   REPODATA0="`cat ${ONE_PKGLISTS_COMPAT}pre | grep -v '^PACKAGE MIRROR'`"
   REPODATA="`echo "$REPODATA0" | tr '\t' ' ' | tr -s ' ' | sed -e 's% $%%' | tr '\n' ' ' | sed -e 's%  %\n%g'| grep -o '.*PACKAGE DESCRIPTION: [^ ]*: ' | sed -e 's%: $%%'`"
   #...each line ex: PACKAGE NAME: zsh-4.3.6-i486-1.tgz PACKAGE LOCATION: ./slackware/ap PACKAGE SIZE (compressed): 2302 K PACKAGE SIZE (uncompressed): 6870 K PACKAGE DESCRIPTION: zsh
   #...that last field is the actual package name without version.
   #the slacky repo has more information in its database, these fields:
   #PACKAGE NAME: PACKAGE LOCATION: PACKAGE SIZE (compressed): PACKAGE SIZE (uncompressed): PACKAGE REQUIRED: PACKAGE CONFLICTS: PACKAGE SUGGESTS: PACKAGE DESCRIPTION:
   echo "$REPODATA" |
   while read DB_ONELINE
   do
    [ "$DB_ONELINE" = "" ] && continue #w016 precaution.
    DB_fullfilename="`echo -n "$DB_ONELINE" | grep -o 'PACKAGE NAME: [^ ]* ' | cut -f 3 -d ' '`"
    DB_path="`echo -n "$DB_ONELINE" | grep -o 'PACKAGE LOCATION: [^ ]* ' | cut -f 3 -d ' ' | sed -e 's%^\./%%'`"
    DB_size="`echo -n "$DB_ONELINE" | grep -o 'PACKAGE SIZE (uncompressed): [^ ]* ' | cut -f 4 -d ' ' | sed -e 's% %%'`"'K'
    DB_dependencies="`echo -n "$DB_ONELINE" | grep -o ' PACKAGE REQUIRED: .* PACKAGE ' | sed -e 's% PACKAGE %%' | cut -f 2-99 -d ' ' | sed -e 's% [^,]*%%g' | sed -e 's%,%,+%g' | sed -e 's/[^0-9a-zA-Z.+_=,-]//g'`" #100626
    [ "$DB_dependencies" != "" ] && DB_dependencies='+'"$DB_dependencies"
    #...processed to ex: +acl,+gcc,+glibc-solibs,+libXt,+libart_lgpl
    DB_nameonly="`echo -n "$DB_ONELINE" | grep -o 'PACKAGE DESCRIPTION: .*' | cut -f 3 -d ' '`"
    #get the actual one-line description...
    dbPATTERN='^'"$DB_nameonly"': '
    DB_description="`grep "$dbPATTERN" ${ONE_PKGLISTS_COMPAT}pre | head -n 1 | cut -f 2 -d '(' | cut -f 1 -d ')' | tr '\-' ' ' | sed -e 's/[^0-9a-zA-Z.+_= ]//g'`"
    #a problem, some entries in slacky db do not have a description in "( .... )" so look further...
    if [ "`grep "$dbPATTERN" ${ONE_PKGLISTS_COMPAT}pre | head -n 1 | grep '('`" = "" ];then
     DB_description="`grep "$dbPATTERN" ${ONE_PKGLISTS_COMPAT}pre | head -n 3 | tail -n 1 | cut -f 2-11 -d ' ' | tr '\-' ' ' | sed -e 's/[^0-9a-zA-Z.+_= ]//g'`"
    fi
    #get the category...
    #w015 find_cat_func replaced by compiled app...
    xDB_description="$DB_description"
    [ "$DB_description" = "" ] && xDB_description="nothing"
    DB_category="`${FIND_CAT} $DB_nameonly "$xDB_description"`"
    #pkgname, version...
    DB_pkgname="`echo -n "$DB_fullfilename" | sed -e 's%-i[3456]86.*%%' | sed -e 's%-noarch.*%%'`"
    noPATTERN="s%${DB_nameonly}%%"
    DB_version="`echo -n "$DB_pkgname" | sed -e "$noPATTERN" | sed -e 's%^\-%%'`"
    #add support for .txz pkgs...
    DB_pkgrelease="`echo -n "$DB_fullfilename" | sed -e 's%\.txz%%' | sed -e 's%\.tgz%%' | sed -e 's%.*\-i[3456]86%%' | sed -e 's%.*\-noarch%%' | sed -e 's%^\-%%'`"
    echo "$DB_pkgname|$DB_nameonly|$DB_version|$DB_pkgrelease|$DB_category|$DB_size|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|" >> $ONE_PKGLISTS_COMPAT
    echo -n "$DB_nameonly "
   done
  ;;
  arch)
   #each line in ${ONE_PKGLISTS_COMPAT}pre is like this:
   #pkgname="acl" pkgver="2.2.47" pkgrel="1" pkgdesc="Library for filesystem ACL support" depends="attr=2.4.41" 
   PKGLISTS_REPO_BASE="`echo -n "${ONE_PKGLISTS_COMPAT}" | rev | cut -f 1 -d '-' | rev`" #core, extra, community
   DB_path="${PKGLISTS_REPO_BASE}/os/i686"
   cat ${ONE_PKGLISTS_COMPAT}pre |
   while read DB_ONELINE
   do
    [ "$DB_ONELINE" = "" ] && continue #w016 bugfix.
    #w016 bugfix, some authors have put trailing tabs (now space) or spaces on the fields, remove...
    DB_nameonly="`echo -n "$DB_ONELINE" | cut -f 2 -d '"' | sed -e 's% $%%'`" #'geany
    DB_version="`echo -n "$DB_ONELINE" | cut -f 4 -d '"' | sed -e 's% $%%'`" #'geany
    DB_pkgrelease="`echo -n "$DB_ONELINE" | cut -f 6 -d '"' | sed -e 's% $%%'`" #'geany
    DB_description="`echo -n "$DB_ONELINE" | cut -f 8 -d '"'`" #'geany
    DB_dependencies="`echo -n "$DB_ONELINE" | cut -f 10 -d '"' | sed -e 's% $%%' | sed -e 's% %,+%g' | sed -e 's%^%+%'`" #'geany
    [ "$DB_dependencies" = "+" ] && DB_dependencies="" #w016 bugfix. some don't have any deps specified.
    DB_pkgname="${DB_nameonly}-${DB_version}"
    #w015 find_cat_func replaced by compiled app...
    xDB_description="$DB_description"
    [ "$DB_description" = "" ] && xDB_description="nothing"
    DB_category="`${FIND_CAT} $DB_nameonly "$xDB_description"`"
    [ "$DB_pkgrelease" != "" ] && xDB_pkgrelease='-'"$DB_pkgrelease"
    DB_fullfilename="${DB_pkgname}${xDB_pkgrelease}ARCHTYPE.pkg.tar.gz" #ARCHTYPE can be '-i686' or nothing
    
    #hack, downloaded a supplementary tarball, extracted info to /tmp/arch_more_fields_pre...
    if [ -s /tmp/arch_more_fields_pre ];then
     ALMOSTFULL="${DB_pkgname}${xDB_pkgrelease}"
     afPATTERN='^'"`echo "$ALMOSTFULL" | sed -e 's%\.%\\.%g' -e 's%\-%\\-%g'`"
     DB_fullfilename="`grep "$afPATTERN" /tmp/arch_more_fields_pre | cut -f 1 -d ' ' | head -n 1`"
     DB_size="`grep "$afPATTERN" /tmp/arch_more_fields_pre | cut -f 2 -d ' ' | head -n 1`"'K'
    fi
    
    echo "$DB_pkgname|$DB_nameonly|$DB_version|$DB_pkgrelease|$DB_category|$DB_size|$DB_path|$DB_fullfilename|$DB_dependencies|$DB_description|" >> $ONE_PKGLISTS_COMPAT
    echo -n "$DB_nameonly "
   done
  ;;
  t2) #w017
   echo "...it's already in the Puppy standard format."
  ;;
  puppy)
   echo "...it's already in the Puppy standard format."
  ;;
 esac
 echo
 #rm -f ${ONE_PKGLISTS_COMPAT}pre 2>/dev/null
 mv -f ${ONE_PKGLISTS_COMPAT}pre /tmp/ 2>/dev/null
 sort --field-separator='|' --key=1 ${ONE_PKGLISTS_COMPAT} > /tmp/$PKGLISTFILE
 mv -f /tmp/$PKGLISTFILE ${ONE_PKGLISTS_COMPAT}
done
echo

if [ "$RUNNINGPUP" = "yes" ];then
 echo
 echo "FINISHED"
 echo "After returning to the main GUI window of the package manager,"
 echo "it will be reading the updated database files."
 echo -n "Press ENTER key to exit: "
 read enditnow
 exit
fi

####################################
#extract all the package names of compat-distro, without version info...
PKGNAMES="`cat $PKGLISTS_COMPAT | cut -f 1,2,3 -d '|'`"
#...on separate lines, ex: gdb-6.8 gdbm-1.8.3 genpower-1.0.5


#check the entries in PKGS_SPECS_TABLE against those in Packages-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}.
#that is, need to verify that pkgs specified in table do actually exist.
echo
echo "Checking that compat-distro pkgs specified in PKGS_SPECS_TABLE actually exist..."
echo "
Checking that compat-distro pkgs specified in PKGS_SPECS_TABLE actually exist..." >> /var/woof/0setup_fail_report_$RUNNINGPUP
FAILCHK="no"
BINARYPARTNAMES="`echo "$PKGS_SPECS_TABLE" | grep '^yes' | cut -f 3 -d '|' | tr ',' '\n' | grep -v '^-' | tr '\n' ' '`"
for ONEBINARYPARTNAME in $BINARYPARTNAMES
do
 #ONEBINARYPARTNAME may match multiple pkgs...
 #i think that dashes need to be backslashed...
 multiPATTERN="`echo -n "$ONEBINARYPARTNAME" | sed -e 's%\\-%\\\\-%g'`"
 #may have blob * wildcard, change to reg.expr....
 multiPATTERN="`echo -n "$multiPATTERN" | sed -e 's%\\*%.*%g'`"
 xPATTERN='|'"$multiPATTERN"'|'
 [ "`echo "$PKGNAMES" | grep "$xPATTERN"`" != "" ] && continue #success.
# if [ "`echo -n "$ONEBINARYPARTNAME" | grep '[a-z]\\-[0-9]'`" != "" ];then
 if [ "`echo -n "$ONEBINARYPARTNAME" | grep '\\*'`" != "" ];then
  zPATTERN='^'"$multiPATTERN"
  [ "`echo "$PKGNAMES" | grep "$zPATTERN"`" != "" ] && continue #success.
 fi
 #failure...
 echo "FAIL: $ONEBINARYPARTNAME"
 echo "FAIL: $ONEBINARYPARTNAME" >> /var/woof/0setup_fail_report_$RUNNINGPUP
 FAILCHK="yes"
done
if [ "$FAILCHK" = "yes" ];then
 echo
 echo "Finished, but some failures. You need to fix the above entries in"
 echo "PKGS_SPECS_TABLE in file DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}."
 echo "Search for suitable packages in these files:"
 echo "`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|'`"
 echo "Or, search online. In the case of Debian or Ubuntu, you can search"
 echo "at packages.debian.org or packages.ubuntu.com."
 echo "After you have fixed PKGS_SPECS_TABLE, run this script again."
 echo "
Finished, but some failures. You need to fix the above entries in variable
PKGS_SPECS_TABLE in file DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}.
Search for suitable packages in these files:
`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|'`
Or, search online. In the case of Debian or Ubuntu, you can search
at packages.debian.org or packages.ubuntu.com.
After you have fixed PKGS_SPECS_TABLE, run this script again."  >> /var/woof/0setup_fail_report_$RUNNINGPUP
else
 echo
 echo "Finished, successful."
 echo "...finished, successful."  >> /var/woof/0setup_fail_report_$RUNNINGPUP
fi

echo
echo "The log of '0setup', as reported above, is saved for future reference."
echo "(In file /var/woof/0setup_fail_report_${RUNNINGPUP})"
echo "In the Woof GUI, tab 'Download dbs', click the 'REPORT' button to"
echo "retrieve this log."
echo -n "Press ENTER key to finish '0setup' script: "
read yepgetout
###END###
