#!/bin/bash
#(c) Copright Barry Kauler July 2010.
#create PET packages of the compat-distro binary pkgs.
#the scripts '2createpackages' and '3builddistro' must have been executed beforehand,
#with the variables in file DISTRO_SPECS set to that of the compatible-distro, for
#example DISTRO_BINARY_COMPAT='t2', DISTRO_FILE_PREFIX='wary', DISTRO_COMPAT_VERSION='8.0rcXorg7.3'.
#after running this script, you will then be able to create a build entirely of
#PET packages, in which case name these DISTRO_BINARY_COMPAT='puppy' and
#DISTRO_COMPAT_VERSION='wary5' -- some generation designation, earlier puppies
#had '3', '4', '5' for this variable.
#100820 replace /var/cache/woof path with /var/local/woof
#100921 remove unecessary entries from DB_dependencies. log db entries to a file.
#110908 update for new woof with support/findpkgs.
#111204 do not use /var/local/woof, instead 'status' directory (better for running from different save-files).
#120719 support raspbian.
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#130306 arch linux: gz now xz.

if [ ! -f 2createpackages ];then
 echo "Must be run from the Woof project directory. Exiting."
 exit
fi

. ./DISTRO_SPECS
[ ! "$DISTRO_DB_SUBNAME" ] && DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #121102 fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.

if [ "${DISTRO_BINARY_COMPAT}" == "puppy" ];then
 echo "DISTRO_BINARY_COMPAT='${DISTRO_BINARY_COMPAT}'. Incorrect usage, aborting."
 exit
fi

if [ ! -f DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #130306
 if [ ! -f DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT} ];then
  echo "File DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}"
  echo "or DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}"
  echo "does not exist. Aborting."
 fi
 exit
fi
if [ -f DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #130306
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
 DPSFILE="DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}"
else
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}
 DPSFILE="DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}"
fi

if [ ! -f status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #110908
 echo "File status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}"
 echo "does not exist. You have not yet run 2createpackages. Aborting."
 exit
fi

BINARIES='deb' #get them from packages-deb.
[ "$DISTRO_BINARY_COMPAT" = "t2" ] && BINARIES="bz2" #get them from packages-bz2-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "slackware" ] && BINARIES="tgz_txz" #100617 download to packages-tgz_txz-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "slackware64" ] && BINARIES="tgz_txz" #100617 download to packages-tgz_txz-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "debian" ] && BINARIES="deb" #download to packages-deb-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "arch" ] && BINARIES="tar_xz" #download to packages-tar_gz-${DISTRO_COMPAT_VERSION}. 130306
[ "$DISTRO_BINARY_COMPAT" = "puppy" ] && BINARIES="pet" #w018 built entirely from pet pkgs.
[ "$DISTRO_BINARY_COMPAT" = "scientific" ] && BINARIES="rpm" #110523 Iguleder: download to packages-rpm-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "mageia" ] && BINARIES="rpm" #110615
[ "$DISTRO_BINARY_COMPAT" = "raspbian" ] && BINARIES="deb_raspbian" #download to packages-deb_raspbian-${DISTRO_COMPAT_VERSION}.
BINARIES="${BINARIES}-${DISTRO_COMPAT_VERSION}" #w478

#110908 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%||$%|%'`"

echo "This script will convert the packages of ${DISTRO_BINARY_COMPAT} version ${DISTRO_COMPAT_VERSION}"
echo "used to build Puppy, into PET packages. These will be placed in directory"
echo "'new-pets'."
echo
echo "This script must be run after running '2createpackages' and '3builddistro'"
echo
echo "The objective is to be able to build a Puppy in Woof entirely from PET"
echo "packages. As such, DISTRO_BINARY_COMPAT variable will become 'puppy'"
echo "(it is currently '${DISTRO_BINARY_COMPAT}'), the DISTRO_COMPAT_VERSION"
echo "variable will become something you choose to designate the generation of"
echo "Puppy, for example 'wary5' (rather than a specific version number)"
echo "(The current value of DISTRO_COMPAT_VERSION is '${DISTRO_COMPAT_VERSION}')"
echo
echo "Type a version/generation name for the proposed Puppy distro,"
echo "this will be put into the 'pet.specs' file inside each PET pkg"
echo "(the first and last chars will be appended onto the PET pkg names)"
echo -n "example: 'wary5' : "
read newDISTRO_COMPAT_VERSION
newDISTRO_BINARY_COMPAT="puppy"

CUT1="`echo -n "$newDISTRO_COMPAT_VERSION" | cut -c 1`"
CUT2="`echo -n "$newDISTRO_COMPAT_VERSION" | rev | cut -c 1`"
CUTnewDISTRO_COMPAT_VERSION="${CUT1}${CUT2}"

#110908...
echo
echo "The PET packages will be created with '${CUTnewDISTRO_COMPAT_VERSION}' appended"
echo "to their names, for example 'abiword-1.2.3-${CUTnewDISTRO_COMPAT_VERSION}.pet'"
echo "If you are happy with this, just press ENTER, or, if you want an"
echo "alternate string (ex: '${CUTnewDISTRO_COMPAT_VERSION}2'), then type the"
echo -n "alternate string now then ENTER: "
read altSTR
[ "$altSTR" != "" ] && CUTnewDISTRO_COMPAT_VERSION="`echo -n "$altSTR" | sed -e 's%^-%%'`"

rm -rf new-pets
mkdir new-pets
echo -n "" > z-new-pets-db

#for checking files that are not really needed...
mkdir layer_top
LAYERFS="aufs"
[ "`lsmod | grep '^unionfs'`" != "" ] && LAYERFS="unionfs"
if [ "$LAYERFS" = "aufs" ];then
 mount -t aufs -o udba=reval,diropq=w,dirs=sandbox3/devx=rw:sandbox3/rootfs-complete=ro layerfs layer_top
else
 mount -t unionfs -o dirs=sandbox3/devx=rw:sandbox3/rootfs-complete=ro layerfs layer_top
fi

#read PKGS_SPECS_TABLE to get all the package names to be converted...
echo
echo "Creating new PETS in new-pets directory..."
#110908 need extra logic here, support optional |pet[:<repo>] or |compat[:<repo>] overrides...
x1COMPAT_GENERIC_NAMES="`echo "$PKGS_SPECS_TABLE" | grep '^yes|' | grep -v '||' | grep -v '|pet|$' | grep -v '|pet:'  | grep -v '|compat|$' | grep -v '|compat:' | cut -f 2 -d '|' | tr '\n' ' '`"
x2COMPAT_GENERIC_NAMES="`echo "$PKGS_SPECS_TABLE" | grep '|compat|$' | cut -f 2 -d '|' | tr '\n' ' '`"
x3COMPAT_GENERIC_NAMES="`echo "$PKGS_SPECS_TABLE" | grep '|compat:' | cut -f 2 -d '|' | tr '\n' ' '`"
COMPAT_GENERIC_NAMES="${x1COMPAT_GENERIC_NAMES} ${x2COMPAT_GENERIC_NAMES} ${x3COMPAT_GENERIC_NAMES}"

for GENERICNAME in $COMPAT_GENERIC_NAMES #ex: 915resolution a52dec abiword ...
do

 #110908 support/findpkgs (called from 2createpackages) has created this file...
 #ex: :a52dec:|compat|Packages-puppy-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||
 gennamePTN=":${GENERICNAME}:" #a pkg may be shared by multiple generic names, ex: :gcc_lib::gcc_dev:|compat|... (ex from Slacko).
 FNDIT="`grep "$gennamePTN" status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | tail -n 1`" #note, maybe more than one hit.
 UPKGVERSION="`echo -n "$FNDIT" | cut -f 6 -d '|'`"

 #overrides...
 case $GENERICNAME in
  x11proto|xorg_base*|xorg73_base*|xserver_xorg*)
   XORGVER="`grep '^xorg-server' sandbox3/rootfs-complete/root/.packages/woof-installed-packages | head -n 1 | cut -f 3 -d '|' | cut -f 1 -d '-' | cut -f 1 -d '_'`"
   #...note, woof-installed-packages also in current dir.
   [ "$XORGVER" = "" ] && XORGVER="1.7" #precaution.
   UPKGVERSION="7.5"
   if vercmp $XORGVER lt 1.7;then
    UPKGVERSION="7.4"
   fi
   if vercmp $XORGVER lt 1.6;then
    UPKGVERSION="7.3"
   fi
  ;;
 esac
 
 for xLOOP in 1 2 3 4
 do
  [ "$xLOOP" == "1" ] && xDEV=""
  [ "$xLOOP" == "2" ] && xDEV="_DEV"
  [ "$xLOOP" == "3" ] && xDEV="_DOC"
  [ "$xLOOP" == "4" ] && xDEV="_NLS"
  [ ! -d packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xDEV} ] && continue
  
  echo "${GENERICNAME}${xDEV}-${UPKGVERSION}"
  if [ -d new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION} ];then
   #take care of odd situation, ex 'perl-html-parser' pkg...
   #cp -a -f --remove-destination packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xDEV}/* new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}/
   continue
  else
   cp -a packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xDEV} new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}
  fi
  if [ $? -ne 0 ];then
   echo " ...error, skipping"
   continue
  fi

  #create the 'pet.specs' file for each new PET package...
  #2createpackages also creates status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} 
  #each line has ${GENERICNAME}|${COMPATPKGNAME} (where COMPATPKGNAME is name of compat-distro pkg)
  COMPATPKGNAMES="`grep "$pGENERICNAME" status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | cut -f 2 -d '|'`"
  COMPATPKGNAME="`echo "$COMPATPKGNAMES" | head -n 1`"
  #file 'woof-installed-packages' has a database entry for COMPATPKGNAME...
  pCOMPATPKGNAME='|'"$COMPATPKGNAME"'|'
  COMPATDBENTRY="`grep "$pCOMPATPKGNAME" sandbox3/rootfs-complete/root/.packages/woof-installed-packages | head -n 1`"
  #pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
  #...'compileddistro|compiledrelease' (fields 11,12) identify where the package was compiled.
  DB_pkgname="`echo -n "$COMPATDBENTRY" | cut -f 1 -d '|'`"
  DB_nameonly="`echo -n "$COMPATDBENTRY" | cut -f 2 -d '|'`"
  DB_version="`echo -n "$COMPATDBENTRY" | cut -f 3 -d '|'`"
  DB_pkgrelease="`echo -n "$COMPATDBENTRY" | cut -f 4 -d '|'`"
  DB_category="`echo -n "$COMPATDBENTRY" | cut -f 5 -d '|'`"
  DB_size="`echo -n "$COMPATDBENTRY" | cut -f 6 -d '|'`"
  DB_path="`echo -n "$COMPATDBENTRY" | cut -f 7 -d '|'`"
  DB_fullfilename="`echo -n "$COMPATDBENTRY" | cut -f 8 -d '|'`"
  DB_dependencies="`echo -n "$COMPATDBENTRY" | cut -f 9 -d '|'`"
  DB_description="`echo -n "$COMPATDBENTRY" | cut -f 10 -d '|'`"
  DB_compileddistro="`echo -n "$COMPATDBENTRY" | cut -f 11 -d '|'`"
  DB_compiledrelease="`echo -n "$COMPATDBENTRY" | cut -f 12 -d '|'`"
  DB_repo="`echo -n "$COMPATDBENTRY" | cut -f 13 -d '|'`"
  #build modified pet.specs file...
  SIZEK="`du -s -k new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION} | cut -f 1 | head -n 1`"
  #for ONECOMPAT in `echo "$COMPATPKGNAMES" | tr '\n' ' '`
  #do
  # #get all the dependencies...
  # pONECOMPAT='|'"$ONECOMPAT"'|'
  # ONEDEPS="`grep "$pONECOMPAT" sandbox3/rootfs-complete/root/.packages/woof-installed-packages | head -n 1 | cut -f 9 -d '|'`"
  # echo -n ""
  #done

  #100921 remove unecessary entries from DB_dependencies...
  LOWDEPS='atk autoconf automake bash bigreqsproto bin86 binutils bison bzip2 cf cmake compositeproto coreutils cpio cups damageproto diffutils embutils expat file findutils fixesproto flex fontcacheproto fontconfig fontsproto freetype gawk gcc gettext giflib glade3 glib glibc glproto grep groff gzip hotplug++ imake inputproto intltool kbproto libdrm libexif libfontenc libice libidl libjpeg libpciaccess libpng libpthread-stubs librsvg libsm libtiff libtool libusb libx11 libxau libxaw libxcb libxcomposite libxcursor libxdamage libxdmcp libxevie libxext libxfixes libxfont libxfontcache libxft libxi libxinerama libxkbfile libxkbui libxmu libxp libxpm libxrandr libxprintapputil libxprintutil libxrender libxres libxscreensaver libxslt libxt libxtrap libtst libxv libxvmc libxxf86dga libxxf86misc libxxf86vm linux-header m4 make man mesa minised mkfontdir mkfontscale mkinitrd mktemp modutils nasm ncurses net-tools openssl pango panoramixproto patch patchutils pciutils pcre perl pixman pkgconfig popt printproto psmisc python randrproto readline recordproto renderproto resourceproto rman scrnsaverproto scrollkeeper sed shadow shared-mime-info subversion sysfiles sysfsutils t1lib tar texinfo trapproto udev usbutils util-linux valgrind videoproto wget xcmiscproto xextproto xf86bigfontproto xf86dgaproto xf86driproto xf86miscproto xf86rushproto xf86vidmodeproto xineramaproto xproto zlib'
  for ONEDEP in $LOWDEPS
  do
   ldP1=',+'"$ONEDEP"','
   ldP2=',+'"$ONEDEP"'$'
   ldP3='+'"$ONEDEP"','
   ldP4='+'"$ONEDEP"'$'
   P1="s%${ldP1}%,%"
   P2="s%${ldP2}%%"
   P3="s%${ldP3}%,%"
   P4="s%${ldP4}%%"
   DB_dependencies="`echo -n "$DB_dependencies" | sed -e "$P1" -e "$P2" -e "$P3" -e "$P4"`"
  done
  DB_dependencies="`echo -n "$DB_dependencies" | tr -s ',' | sed -e 's%,$%%' -e 's%^,%%'`"
  
  case $xDEV in
   _DEV) DB_dependencies="+${GENERICNAME}" ;;
   _DOC) DB_dependencies="" ;;
   _NLS) DB_dependencies="+${GENERICNAME}" ;;
  esac
  
  echo "${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}|${GENERICNAME}${xDEV}|${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}||${DB_category}|${SIZEK}K||${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}.pet|${DB_dependencies}|${DB_description}|${newDISTRO_BINARY_COMPAT}|${newDISTRO_COMPAT_VERSION}||" > new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}/pet.specs
 
  #100921 log db entries to a file...
  echo "${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}|${GENERICNAME}${xDEV}|${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}||${DB_category}|${SIZEK}K||${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}.pet|${DB_dependencies}|${DB_description}|${newDISTRO_BINARY_COMPAT}|${newDISTRO_COMPAT_VERSION}||" >> z-new-pets-db
 
  #remove files that will get deleted anyway...
  if [ "$xDEV" == "" -o "$xDEV" == "_DEV" ];then
   pFIX="s%new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}/%%"
   for ONESPEC in `find new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION} -mindepth 2 | tr '\n' ' '`
   do
    [ ! -e $ONESPEC ] && continue
    DOWNSPEC="`echo -n "$ONESPEC" | sed -e "$pFIX"`"
    if [ ! -e layer_top/${DOWNSPEC} ];then
     echo "Deleting new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}/${DOWNSPEC}"
     rm -rf new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}/${DOWNSPEC}
    fi
   done
  fi
 
  cd new-pets
  sync
  dir2tgz ${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}
  tgz2pet ${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}.tar.gz
  cd ..
  #rm -rf new-pets/${GENERICNAME}${xDEV}-${UPKGVERSION}-${CUTnewDISTRO_COMPAT_VERSION}
 done

done
echo

sync
umount layer_top
rmdir layer_top
###END###

