#!/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, 2compat2pets, and 2createpackages,
#which created 'packages-t2test' (the same thing as 'packages-wary'). the t2 setup also created 'Packages-t2-9-official'.
#then ran 3builddistro, then 3compat2pets, got all T2 pkgs converted to pets in dir 'new-pets'
#and dbs in file 'z-new-pets-db'.
#now run '3mergepets' to merge pets in 'new-pets' into those in ../pet-construction/pet_packages-wary5-WARY52,
#(pet_packages-wary5-WARY52 is a copy of pet_packages-wary5)
#note, earlier scripts are usable to convert any compat-distro pkgs to pets, but this one is
#specific to wary, needs mods to work for other compat-distro.

echo "This script will merge imported pets in dir 'new-pets' into dir"
echo "'pet_packages-wary5-WARY5' (which is a copy of pet_packages-wary5)."
echo -n "Press ENTER to continue: "
read yesgo

if [ ! -f z-new-pets-db ];then
 echo "ERROR: file 'z-new-pets-db' does not exist. Aborting."
 exit
fi
if [ ! -d new-pets ];then
 echo "ERROR: dir 'new-pets' does not exist. Aborting."
 exit
fi

#echo -n "" > Packages-puppy-wary5-official-WARY52
cp -f Packages-puppy-wary5-official Packages-puppy-wary5-official-WARY52

rm -f MERGEPETS_ERROR_MERGE 2>/dev/null
rm -f MERGEPETS_WARNING_OLD_VERSIONS 2>/dev/null
echo "Need to manually check contents against originals..." > MERGEPETS_WARNING_NEW_VERSIONS
echo
echo "deleting any 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
rm -f ../pet-construction/pet_packages-wary5-WARY52/nohup.out 2>/dev/null
sync
echo "...done"

#db fields: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|

cat z-new-pets-db |
while read ALINE
do
 DB_pkgname1="`echo -n "$ALINE" | cut -f 1 -d '|'`"
 DB_nameonly1="`echo -n "$ALINE" | cut -f 2 -d '|'`"
 DB_version1="`echo -n "$ALINE" | cut -f 3 -d '|'`"
 DB_fullfilename1="`echo -n "$ALINE" | cut -f 8 -d '|'`"
 nPTN1="|${DB_nameonly1}|"
 
 DB_version2="`grep "$nPTN1" Packages-puppy-wary5-official | tail -n 1 | cut -f 3 -d '|'`"
 [ "$DB_version2" = "" ] && DB_version2='0.0.0.1'
  
 NAMEONLY="$DB_nameonly1"
 NEWNAME="$DB_pkgname1"
 NEWVERSION="${DB_version1}"
 NEWNAMEONLY="${NAMEONLY}"
 NEWPET="${DB_fullfilename1}"
 DB_fullfilenamex="`grep "$nPTN1" Packages-puppy-wary5-official | tail -n 1 | cut -f 8 -d '|' | sed -e 's%\.pet$%%'`"

 if [ -d new-pets/${DB_pkgname1} -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
     rm -rf ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME} 2>/dev/null #precaution.
     mv -f ../pet-construction/pet_packages-wary5-WARY52/${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} -mindepth 1 -type f | sed -e "$sPTN" |
     while read ONEFND
     do
      ONEDIR="`dirname "$ONEFND"`"
      if [ -f "new-pets/${DB_pkgname1}/${ONEFND}" ];then
       echo "  ${ONEFND}"
       if [ -h "new-pets/${DB_pkgname1}/${ONEFND}" ];then
        echo "ERROR HYPERLINK, EXPECTED FILE: new-pets/${DB_pkgname1}/${ONEFND}"
        echo "ERROR HYPERLINK, EXPECTED FILE: new-pets/${DB_pkgname1}/${ONEFND}" >> MERGEPETS_ERROR_MERGE
       else
        cp -a -f --remove-destination "new-pets/${DB_pkgname1}/${ONEFND}" "../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/${ONEDIR}/"
       fi
      else
       echo "ERROR MISSING FILE: new-pets/${DB_pkgname1}/${ONEFND}"
       echo "ERROR MISSING FILE: new-pets/${DB_pkgname1}/${ONEFND}" >> MERGEPETS_ERROR_MERGE
      fi
     done
     #fix pet.specs...
     #db fields: pkgname|nameonly|version|pkgrelease|category|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 '|'`"
     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
     
     #extra checking...
     ePTN="s%new-pets/${DB_pkgname1}/%%"
     find new-pets/${DB_pkgname1} -mindepth 1 | sed -e "$ePTN" |
     while read ONEFND
     do
      if [ ! -e ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/${ONEFND} ];then
       case $NEWNAME in
        *_DEV-*)
         if [ -h ../pet-construction/pet_packages-wary5-WARY52/${NEWNAME}/${ONEFND} ];then
          [ "`echo -n "$ONEFND" | rev | cut -c 1,2,3 | rev`" = ".so" ] && continue
         fi
        ;;
       esac
       echo "WARNING, DOES NOT EXIST IN TARGET: new-pets/${DB_pkgname1}/${ONEFND}"
       echo "WARNING, DOES NOT EXIST IN TARGET: new-pets/${DB_pkgname1}/${ONEFND}" >> MERGEPETS_ERROR_MERGE
      fi
     done
    else
    
     if vercmp $DB_version1x gt $DB_version2x;then
      
      echo "UPDATED PKG: ${NEWNAME}"
      echo "PKG:${NAMEONLY} NEW:${DB_version1x} OLD:${DB_version2x}" >> MERGEPETS_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.
      rm -rf ../pet-construction/pet_packages-wary5-WARY52/${DB_pkgname1} 2>/dev/null #precaution
      cp -a new-pets/${DB_pkgname1} ../pet-construction/pet_packages-wary5-WARY52/

      #db fields: pkgname|nameonly|version|pkgrelease|category|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 '|'`"
      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
     
     else
      echo "WARNING: ${DB_pkgname1} older version, not merged."
      echo "WARNING: ${DB_pkgname1} older version, not merged." >> MERGEPETS_WARNING_OLD_VERSIONS
     fi
     
    fi
  
   else
    #does not exist in pet_packages-wary5-WARY52, just copy it straight through...
    echo "Straight thru: ${DB_pkgname1}"
    rm -rf ../pet-construction/pet_packages-wary5-WARY52/${DB_pkgname1} 2>/dev/null #precaution
    cp -a new-pets/${DB_pkgname1} ../pet-construction/pet_packages-wary5-WARY52/
    grep -v "$nPTN1" Packages-puppy-wary5-official-WARY52 > /tmp/ppwo52
    mv -f /tmp/ppwo52 Packages-puppy-wary5-official-WARY52
    cat new-pets/${DB_pkgname1}/pet.specs >> Packages-puppy-wary5-official-WARY52
   fi
  
 else
  #does not exist in pet_packages-wary5-WARY52, just copy it straight through...
  echo "Straight thru: ${DB_pkgname1}"
  rm -rf ../pet-construction/pet_packages-wary5-WARY52/${DB_pkgname1} 2>/dev/null #precaution
  cp -a new-pets/${DB_pkgname1} ../pet-construction/pet_packages-wary5-WARY52/
  grep -v "$nPTN1" Packages-puppy-wary5-official-WARY52 > /tmp/ppwo52
  mv -f /tmp/ppwo52 Packages-puppy-wary5-official-WARY52
  cat new-pets/${DB_pkgname1}/pet.specs >> Packages-puppy-wary5-official-WARY52
 fi
 sync
 
done

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

###END###
