#!/bin/sh
#sept 2011. creating wary 5.2pre, with new pkgs compiled in t2, need to upgrade all the existing ones.
#note, i previously setup woof for building a t2 system and ran 0pre, 0setup and 2createpackages,
#which created 'packages-t2test' (the same thing as 'packages-wary'), then i changed back to a wary build.
#the t2 setup also created 'Packages-t2-9-official'.
#120812 db category now allows optional subcategory (for which an icons exists in /usr/local/lib/X11/mini-icons).

echo "Need to manually check contents again originals..." > WARNING_NEW_VERSIONS

echo "deleting prior ../pet-construction/pet_packages-wary5-WARY52 ..."
rm -rf ../pet-construction/pet_packages-wary5-WARY52 2>/dev/null
sync
echo "...done"
mkdir -p ../pet-construction/pet_packages-wary5-WARY52

echo "Copying all old wary pkgs to ../pet-construction/pet_packages-wary5-WARY52 ..."
cp -a ../pet-construction/pet_packages-wary5/* ../pet-construction/pet_packages-wary5-WARY52/
sync
rm -f ../pet-construction/pet_packages-wary5-WARY52/*.pet
sync
echo "...done"

cp -f Packages-puppy-wary5-official Packages-puppy-wary5-official-WARY52

#120812 subcategory...
#db fields: pkgname|nameonly|version|pkgrelease|category[;subcategory]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|

rm -f t2laterversions 2>/dev/null

cat Packages-t2-9-official |
while read ALINE
do
 DB_nameonly1="`echo -n "$ALINE" | cut -f 2 -d '|'`"
 DB_version1="`echo -n "$ALINE" | cut -f 3 -d '|'`"
 nPTNexe="|${DB_nameonly1}|"
 nPTNdev="|${DB_nameonly1}_DEV|"
 nPTNdoc="|${DB_nameonly1}_DOC|"
 nPTNnls="|${DB_nameonly1}_NLS|"
 
 for nPTN1 in $nPTNexe $nPTNdev $nPTNdoc $nPTNnls
 do
  DB_version2="`grep "$nPTN1" Packages-puppy-wary5-official | tail -n 1 | cut -f 3 -d '|'`"
  [ "$DB_version2" = "" ] && continue
  
  NAMEONLY="`echo -n "$nPTN1" | tr -d '|'`" #convert ex1: |abiword| to abiword ex2: |abiword_DEV| to abiword_DEV
  DB_fullfilenamex="`grep "$nPTN1" Packages-puppy-wary5-official | tail -n 1 | cut -f 8 -d '|' | sed -e 's%\.pet$%%'`"
  #NEWNAME="`echo -n "$DB_fullfilenamex" | sed -e 's%$%-w52%' -e 's%-w5-w52$%-w52%'`" #append -w52 to pkg name.
  NEWNAME="`echo -n "$DB_fullfilenamex" | sed -e 's%-w5$%-w52%'`" #change -w5 to -w52
  if [ -d packages-t2test/${NAMEONLY} -a "$DB_fullfilenamex" ];then
  
   if [ -d ../pet-construction/pet_packages-wary5/${DB_fullfilenamex} ];then
    echo "$NAMEONLY"
    #truncate versions to same length...
    DB_version2x="`echo -n "$DB_version2" | cut -f 1 -d '-' | cut -f 1 -d '_'`"
    DB_version1x="`echo -n "$DB_version1" | cut -f 1 -d '-' | cut -f 1 -d '_'`"
    if vercmp $DB_version1x eq $DB_version2x;then
     #copy the original pkg to a new place...
     rm -rf ../pet-construction/pet_packages-wary5-WARY52/${DB_fullfilenamex} #get rid of old one.
     cp -a ../pet-construction/pet_packages-wary5/${DB_fullfilenamex} ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}
     #now copy the files from the t2 pkgs...
     sPTN="s%../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/%%"
     find ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME} -type f | sed -e "$sPTN" |
     while read ONEFND
     do
      ONEDIR="`dirname "$ONEFND"`"
      #mkdir -p ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/${ONEDIR}
      if [ -f "packages-t2test/${NAMEONLY}/${ONEFND}" ];then
       echo "  ${ONEFND}"
       cp -a -f --remove-destination "packages-t2test/${NAMEONLY}/${ONEFND}" "../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/${ONEDIR}/"
      fi
     done
     #fix pet.specs...
     NEWSPECS="`cat ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/pet.specs | tr -d '\n' | tr '|' '\n' | sed -e 's%-w5$%-w52%' -e 's%-w5\.pet$%-w52.pet%' | tr '\n' '|'`"
     grep -v "$nPTN1" Packages-puppy-wary5-official-WARY52 > /tmp/ppwo52
     mv -f /tmp/ppwo52 Packages-puppy-wary5-official-WARY52
     echo "$NEWSPECS" >> Packages-puppy-wary5-official-WARY52
     echo "$NEWSPECS" > ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/pet.specs
    else
    
     if vercmp $DB_version1x gt $DB_version2x;then
      #more tricky, version has changed...
      NEWNAME="${NAMEONLY}-${DB_version1}-w52" #ex: abiword-2.9.1-w52
      NEWVERSION="${DB_version1}-w52"
      NEWNAMEONLY="${NAMEONLY}"
      NEWPET="${NEWNAME}.pet"
      
      echo "UPDATED PKG: ${NEWNAME}"
      echo "PKG:${NAMEONLY} NEW:${DB_version1x} OLD:${DB_version2x}" >> WARNING_NEW_VERSIONS

      #copy the t2 pkg to a new place...
      mv -f ../pet-construction/pet_packages-wary5-WARY52/${DB_fullfilenamex} ../pet-construction/pet_packages-wary5-WARY52/${DB_fullfilenamex}OLDWARY5PKG #rename the old one. kept for manual comparison.
      cp -a packages-t2test/${NAMEONLY} ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}

      #db fields: pkgname|nameonly|version|pkgrelease|category[;subcategory]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
      #the t2 spec is $ALINE, however, get it from old wary...
      OLDSPEC="`grep "$nPTN1" Packages-puppy-wary5-official | tail -n 1`"
      DB_category="`echo -n "$OLDSPEC" | cut -f 5 -d '|' | cut -f 1 -d ';'`" #120812
      DB_size="`echo -n "$ALINE" | cut -f 6 -d '|'`"
      DB_dependencies="`echo -n "$OLDSPEC" | cut -f 9 -d '|'`"
      DB_description="`echo -n "$OLDSPEC" | cut -f 10 -d '|'`"
      NEWSPECS="${NEWNAME}|${NEWNAMEONLY}|${NEWVERSION}||${DB_category}|${DB_size}||${NEWPET}|${DB_dependencies}|${DB_description}|puppy|wary5||"
      grep -v "$nPTN1" Packages-puppy-wary5-official-WARY52 > /tmp/ppwo52
      mv -f /tmp/ppwo52 Packages-puppy-wary5-official-WARY52
      echo "$NEWSPECS" >> Packages-puppy-wary5-official-WARY52
      echo "$NEWSPECS" > ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/pet.specs
     
     fi
     
    fi
  
   else
    echo "WARNING: packages-t2test/${NAMEONLY} DOES NOT EXIST"
   fi
  fi
  sync
 done
 
done

sort Packages-puppy-wary5-official-WARY52 > /tmp/ppwo52
mv -f /tmp/ppwo52 Packages-puppy-wary5-official-WARY52



###END###
