#!/bin/sh
#(c) Copyright Barry Kauler 2009, puppylinux.com
#this script finds all builtin packages in Puppy Linux. called by 3builddistro.
#110821 completely rewritten.
#111204 do not use /var/local/woof, instead 'status' directory (better for running from different save-files).
#121028 exclude pkgs that go into devx only from woof-installed-packages.
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#131209 Packages-puppy- files renamed to Packages-pet-
#141121 script 'extract4quirky' (see support/buildroot) created audit/buildroot-pkgs-list, buildroot-pkgs-list-versioned, insert into woof-installed-packages.
#141231 do not call support/findpkgs

export LANG=C

. ./DISTRO_SPECS #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION
[ ! "$DISTRO_DB_SUBNAME" ] && DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #121102 fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.
if [ -f ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #w478
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} #has FALLBACKS_COMPAT_VERSIONS
else
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT} #has FALLBACKS_COMPAT_VERSIONS
fi

echo "Creating file woof-installed-packages..."

#remove comments from PKGS_SPECS_TABLE... make sure '|' on end... get rid of old |+udev,+whatever on end...
PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep -v '^#' | grep -v '^$' | tr '\t' ' ' | tr -s ' ' | tr '+' '&' | sed -e 's%|&.*%%' | tr '&' '+' | sed -e 's% #.*%%' -e 's% $%%' -e 's%$%|%' -e 's%||$%|%'`"

#141231 this file is created in call to support/findpkgs in 2createpackages. not appropriate to do so again...
##new script to find all pkgs for build...
#./support/findpkgs
##...returns file status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
## each line is full db entry for pkg, prefixed with :generic-name:, pet/compat and repo-filename.
## ex: :a52dec:|compat|Packages-pet-wary5-official|a52dec-0.7.4-w5|a52dec|0.7.4-w5||BuildingBlock|68K||a52dec-0.7.4-w5.pet||A free ATSC A52 stream decoder|puppy|wary5||
#if [ $? -ne 0 ];then
# echo
# echo "ERROR: Script support/findpkgs aborted with an error, exiting."
# exit 1
#fi
if [ ! -f status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
 echo
 #echo "ERROR: Something went wrong with support/findpkgs, exiting."
 echo "ERROR:"
 echo "Missing file: status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}"
 echo "This is created in support/findpkgs called from 2createpackages. Exiting."
 exit 1
fi

#need to find exactly what has gone into the build and the devx...
echo -n "" > /tmp/woof-installed-packages-tmp
echo -n "" > /tmp/devx-only-installed-packages-tmp #121028
cat status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} |
while read ONELINE
do
 #ex: :a52dec:|compat|Packages-pet-wary5-official|a52dec-0.7.4-w5|a52dec|0.7.4-w5||BuildingBlock|68K||a52dec-0.7.4-w5.pet||A free ATSC A52 stream decoder|puppy|wary5||
 ADBENTRY="`echo -n "$ONELINE" | cut -f 4-19 -d '|'`"
 GENERICNAMES="`echo -n "$ONELINE" | cut -f 1 -d '|' | tr ':' ' '`"
 NAMEONLY="`echo -n "$ADBENTRY" | cut -f 2 -d '|'`"
 case $NAMEONLY in
  *_DEV)
   PTN1='^dev$'
   PTN2='dev>exe$'
  ;;
  *_DOC)
   PTN1='^doc>exe$'
   PTN2='^doc>dev$'
  ;;
  *_NLS)
   PTN1='^nls>exe$'
   PTN2='^nls>dev$'
  ;;
  *) #allow all thru...
   PTN1=''
   PTN2=''
  ;;
 esac
 PTN3='exe>dev$' #121028
 for AGENERICNAME in $GENERICNAMES
 do
  gnPTN="^yes|${AGENERICNAME}|"
  SPLITUPS="`echo "$PKGS_SPECS_TABLE" | grep "$gnPTN" | cut -f 4 -d '|' | tr ',' '\n'`"
  FLG3="`echo "$SPLITUPS" | grep "$PTN3"`" #121028
  if [ "$FLG3" ];then #121028
   echo "$ADBENTRY" >> /tmp/devx-only-installed-packages-tmp
   break
  fi
  FLG1="`echo "$SPLITUPS" | grep "$PTN1"`"
  FLG2="`echo "$SPLITUPS" | grep "$PTN2"`"
  sumFLGS="${FLG1}${FLG2}"
  if [ "$sumFLGS" ];then
   echo "$ADBENTRY" >> /tmp/woof-installed-packages-tmp
   break
  fi
 done
done
sync

#grep '|pet|' status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | cut -f 4-19 -d '|' > /tmp/woof-installed-packages-pet
#sync
#grep '|compat|' status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | cut -f 4-19 -d '|' > /tmp/woof-installed-packages-compat
#sync
#
##sort alphabetically...
#sort --key=1 --field-separator="|" /tmp/woof-installed-packages-pet > /tmp/petget_woof-installed-packages
#mv -f /tmp/petget_woof-installed-packages /tmp/woof-installed-packages-pet
#sort --key=1 --field-separator="|" /tmp/woof-installed-packages-compat > /tmp/petget_woof-installed-packages
#mv -f /tmp/petget_woof-installed-packages /tmp/woof-installed-packages-compat

#141121
#script 'extract4quirky' (see support/buildroot) created audit/buildroot-pkgs-list, buildroot-pkgs-list-versioned
#replace '0buildroot' entry in woof-installed-packages with full list of pkgs...
if [ "$(grep '^0buildroot' /tmp/woof-installed-packages-tmp)" != "" ];then
 echo -n "" > /tmp/woof-installed-packages-tmpx
 CNTA=1
 for PKGNV in `cat packages-${DISTRO_FILE_PREFIX}/0buildroot/audit/buildroot-pkgs-list-versioned | tr '\n' ' '` #ex: libjpeg-1.2.3
 do
  PKGNO="$(cat packages-${DISTRO_FILE_PREFIX}/0buildroot/audit/buildroot-pkgs-list | head -n ${CNTA} | tail -n 1)" #ex: libjpeg
  CNTA=`expr $CNTA + 1`
  PKGVO="$(echo -n "$PKGNV" | sed -e "s%^${PKGNO}-%%")"
  #i may have used a pet that overrides pkg builtin to 0builddistro...
  PTN1="|${PKGNO}|"
  [ "$(grep "${PTN1}" /tmp/woof-installed-packages-tmp)" != "" ] && continue
  #db fields: pkgname|nameonly|version|pkgrelease|category[;subcategory]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
  echo "${PKGNV}|${PKGNO}|${PKGVO}||BuildingBlock|0K||||this pkg is inside 0buildroot|${DISTRO_BINARY_COMPAT}|${DISTRO_COMPAT_VERSION}||" >> /tmp/woof-installed-packages-tmpx
 done
 cat /tmp/woof-installed-packages-tmpx >> /tmp/woof-installed-packages-tmp
fi

sort --key=1 --field-separator="|" /tmp/woof-installed-packages-tmp > woof-installed-packages
if [ -s /tmp/devx-only-installed-packages-tmp ];then
 sort --key=1 --field-separator="|" /tmp/devx-only-installed-packages-tmp > devx-only-installed-packages
fi

###END###
