#!/bin/bash

dofetch() {
  COMMAND=${1//'${URI}'/$2}
  COMMAND=${COMMAND//'${DISTDIR}'/$3}
  $COMMAND
}

declare -i lastpatchdistance patchdistance
getbestpatchpath() {
  local i lastpatchpath packagelist filetoobtain lastpatchdistance=1024
  filetoobtain=${1%% *}
  packagelist=$1
   #  echo "${dtulist[i]}: ${oldlist[i]} > ${newlist[i]}";
  for i in $(seq $numallpatches); do
    if [ "${newlist[i]}" == "$filetoobtain" ] && 
         [ "$packagelist" == "${packagelist%${oldlist[i]}*}" ]
    then
#      echo "found update to $filetoobtain"
      for repo in $PACKAGE_PATH; do
#        if [ -f "$REPOSITORY/${oldlist[i]}" ]; then
        if [ -f "$repo/${oldlist[i]}" ]; then
          patchdistance=1
          patchpath=${dtulist[i]} 
	  return;
        fi
      done
        getbestpatchpath "${oldlist[i]} $packagelist"
	#echo "getbestpatchpath $recursiondepth returned: $patchpath $patchdistance"
        if [ $patchdistance -lt $lastpatchdistance ]; then
          lastpatchdistance=$patchdistance
	  lastpatchpath="$patchpath ${dtulist[i]}";
        fi
	#echo "$lastpatchpath $lastpatchdistance"
	#echo

    fi

  done
  patchdistance=$lastpatchdistance+1
  patchpath=$lastpatchpath
#  echo "return $patchdistance $patchpath $recursiondepth"
}

add_dtu_upgrade () {
  let "numallpatches+=1"
  oldlist[numallpatches]=$1
  newlist[numallpatches]=$2
  dtulist[numallpatches]=$3
}

if [ -z "$2" ]; then
  echo "must pass two options: download link and destination"
  exit 1;
fi
REPOSITORY=$2               #local distfiles folder
MIRROR=`dirname $1`         #download URL
NEWFILE=`basename $1`       #name of desired package
echo "need to obtain $NEWFILE"
declare -a oldlist newlist dtulist
declare -i numallpatches
while read format option1 option2 option3
do
  case $format in
    "0")
    	add_dtu_upgrade $option1 $option2 $option3
	;;
    "1")
    	add_dtu_upgrade $option1 $option2 ${option1%.t*}.fdtu
	;;
    "2")
    	add_dtu_upgrade $option2 $option1 ${option2%.t*}.bdtu
	;;
  esac
done
if [ ! -z $numallpatches ]; then getbestpatchpath $NEWFILE; fi

if [ -z "$patchpath" ]; then
  echo "Couldn't find old package to update."
  exit 2;
fi

echo "will try patch sequence: $patchpath"
for patchtotry in $patchpath; do
  URI=$MIRROR/$patchtotry; DISTDIR=$REPOSITORY
  if [ -f "$REPOSITORY/$patchtotry" ]; then
#    retcode=`edelta -pi $REPOSITORY/$patchtotry`
#    if [ ! "${retcode%truncated}" = "$retcode" ]; then
      echo "continue download";
      dofetch "$PATCH_RESUMECOMMAND" $URI $DISTDIR
#    else
#      echo "have patch $patchtotry";
#    fi
  else
    echo "downloading patch $patchtotry"
    dofetch "$PATCH_FETCHCOMMAND" $URI $DISTDIR
    if [ $? -ne 0 ]; then
      echo "Couldn't find patch for update"
      exit 3;
    fi;
  fi;
  patchOK=`edelta -pi $REPOSITORY/$patchtotry`
  if [ -z "$patchOK" ]; then
    rm $REPOSITORY/$patchtotry
    echo "Corrupt patch."
    exit 4;
  fi
done
cd $REPOSITORY
echo "Apply patch(es)"
deltup -prD "$PACKAGE_PATH" $patchpath
if [ ! -f $REPOSITORY/$NEWFILE ]; then
  echo "Patching failed."
  exit 5;
fi
rm $patchpath


syntax highlighted by Code2HTML, v. 0.9.1