#!/bin/sh
#BK sept 2011
#this script is used in conjunction with '3compat2pets'
#run this script before '2createpackages'.
#then run, in this order: 2createpackages, 3builddistro, 3compat2pets
#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

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" = "debian" ] && BINARIES="deb" #download to packages-deb-${DISTRO_COMPAT_VERSION}.
[ "$DISTRO_BINARY_COMPAT" = "arch" ] && BINARIES="tar_xz" #download to packages-tar_xz-${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

if [ ! -d packages-${BINARIES} ];then
 echo "Directory packages-${BINARIES} does not exist. Aborting."
 exit
fi

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


#110908 go the whole hog, convert every compat-pkg to a pet...
echo
echo "This script is used in conjunction with 3compat2pets."
echo "The default is to only convert compat-pkgs that have a 'yes' entry in"
echo "variable PKGS_SPECS_TABLE (in file ${DPSFILE})"
echo "Just press ENTER key to accept default."
echo "Or, if you would like in addition to convert every single package in"
echo "directory packages-${BINARIES} into a PET pkg, press any printable"
echo "key then ENTER."
echo "NOTICE: if you choose to convert all compat-pkgs, file"
echo " ${DPSFILE}"
echo " will be modified and the original copied with -ORIG appended."
echo " You will probably want to restore the original file after this exercise."
echo -n "Type ENTER or any printable-char then ENTER: "
read extraflag
if [ "$extraflag" = "" ];then
 echo "...ok, 'yes' pkgs only"
 echo "Leaving file ${DPSFILE} as-is."
 extraPST=""
else
 echo "...ok, convert everything."
 extraPST="`ls -1 packages-${BINARIES}  | sed -e 's%\.tar\.bz2$%%' -e 's%\.rpm$%%' -e 's%\.tgz$%%' -e 's%\.txz$%%' -e 's%\.deb$%%' | sed -e 's%\-[0-9].*$%%' | sed -r -e 's%(.*)%\1|\1%' | sed -e 's%^%yes|%' -e 's%$%|exe,dev,doc,nls|%'`"
 xPKGS_SPECS_TABLE="${PKGS_SPECS_TABLE}
${extraPST}"
 zPKGS_SPECS_TABLE="`echo "$xPKGS_SPECS_TABLE" | grep '^yes' | sort --field-separator="|" --key=2,3 -u`"

 #maybe duplicate conflicting entries...
 DUPLICATES="`echo "$zPKGS_SPECS_TABLE" | cut -f 2 -d '|' | sort | uniq -d | sort -u | tr '\n' ' '`" #-d option, return duplicates only.
 for ONEDUP in $DUPLICATES
 do
  nPTN="yes|${ONEDUP}|"
  extraPST="`echo "$extraPST" | grep -v "$nPTN"`"
 done
 xPKGS_SPECS_TABLE="${PKGS_SPECS_TABLE}
${extraPST}"
 PKGS_SPECS_TABLE="`echo "$xPKGS_SPECS_TABLE" | grep '^yes' | sort --field-separator="|" --key=2,3 -u`"
 
 yesdoit=""
 if [ -f ${DPSFILE}-ORIG ];then
  echo
  echo "File ${DPSFILE} ${DPSFILE}-ORIG"
  echo "already exists. Overwrite it?"
  echo -n "ENTER only for yes: "
  read yesdoit  
 fi
 if [ "$yesdoit" = "" ];then
  mv -f ${DPSFILE} ${DPSFILE}-ORIG
 fi
 echo "FALLBACKS_COMPAT_VERSIONS='${FALLBACKS_COMPAT_VERSIONS}'" > ${DPSFILE}
 echo "PKGS_SPECS_TABLE='${PKGS_SPECS_TABLE}'" >> ${DPSFILE}
 echo "Modified ${DPSFILE}"

fi

echo
echo "Done. Now run 2createpackages, 3builddistro, 3compat2pets"
###END###
