#!/bin/bash
#
# autodist-tool
# Copyright (c) 2008-2014 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
. /etc/autodist/config
me=(${0##*/} $VERSION "Tue Oct 28 2008")

DATE_NOW=`date +%s`
RPMDIR=/usr/src/RPM/
AUTOUPDATEDIR=/var/ftp/pub/openmamba/$AUTODIST_REPOSITORY/autoupdate/
BUILDDEPSFILE=/var/ftp/pub/openmamba/distromatic/$AUTODIST_REPOSITORY/builddeps-i586
SOURCESDIR=$AUTOUPDATEDIR/sources/
SRCPKGLIST=/var/ftp/pub/openmamba/$AUTODIST_REPOSITORY/srcpkglist
SUCCESSLISTDIR=$AUTOUPDATEDIR
SKIPPEDLISTDIR=$AUTOUPDATEDIR

#eval `autospec -q --eval="srpms_dir"`
#eval `autospec -q --eval="rpms_dir"`
eval `autospec -q --eval="spec_dir"`
#eval `autospec -q --eval="source_dir"`
#eval `autospec -q --eval="tmppath_dir"`

function usage() {
   echo "\
${me[0]} ${me[1]}
"$"Copyright (C) 2008 Silvan Calarco <silvan.calarco@mambasoft.it>""
"$"Released under the terms of the GNU GPL v3 license"
   echo "

"$"Usage"":
   $me makepatch package [auto|build|update|remove]
   $me schedule job_regexp..
   $me unschedule job_regexp..
   $me skip job_regexp..
"

}

[ $# -gt 0 ] || { usage ; exit 1; }

unset OPERATION COMMAND PKG

for ((i=1; i<=$#; i++)); do
   case ${!i} in
      -h|--help) usage; exit 1 ;;
      *) if [ ! "$COMMAND" ]; then
            COMMAND=${!i}
	 elif [ ! "$JOB" ]; then
	    JOB=${!i}
	 else
	    OPERATION=${!i}
	 fi
	 ;;
   esac
done

[ "${JOB/*\#}" != "${JOB}" ] && {
   JOBVER="+${JOB/*\#}" 
   JOB="${JOB/\#*}"
} || JOBVER="+0"

if [ "$COMMAND" = "makepatch" ]; then
   [ "$JOB" ] || {
      echo "Error: package non specified."
      exit 1
   }

   if [ ! "$OPERATION" ]; then
     [ -e $AUTOUPDATEDIR/spec-patches-update/$JOB.spec.patch ] && {
         echo "== update patch =="
         cat $AUTOUPDATEDIR/spec-patches-update/$JOB.spec.patch
      }
      [ -e $AUTOUPDATEDIR/spec-patches-build/$JOB.spec.patch ] && {
         echo "== build patch =="
         cat $AUTOUPDATEDIR/spec-patches-build/$JOB.spec.patch
      }
      exit 0
   elif [ "$OPERATION" = "remove" ]; then
      echo -n "== Removing patches for $JOB package"
      rm -f $AUTOUPDATEDIR/spec-patches-update/$JOB.spec.patch
      rm -f $AUTOUPDATEDIR/spec-patches-build/$JOB.spec.patch
      echo
      exit 0
   elif [ "$OPERATION" = "auto" ]; then
      if [ -e $spec_dir/.$JOB.spec.autodist.prebuild -a ! -e $AUTOUPDATEDIR/spec-patches-update/$JOB.spec.patch ]; then
         OPERATION=build
      elif [ -e $spec_dir/.$JOB.spec.autodist.preupdate -a ! -e $AUTOUPDATEDIR/spec-patches-build/$JOB.spec.patch ]; then
         OPERATION=update
      else
         echo "Error: unable to autodetect patch type."
         exit 1
      fi
   fi

   if [ ! -e $spec_dir/.$JOB.spec.autodist.pre$OPERATION ]; then
      echo "Error: pre$OPERATION specfile missing; make sure you prepare and update the package using autodist."
      exit 1
   fi

   if [ "$OPERATION" = "build" -a -e $AUTOUPDATEDIR/spec-patches-update/$JOB.spec.patch ]; then
      echo "Error: trying to make a build patch when an update patch already exists! Remove or create a update patch instead."
      exit 1
   elif [ "$OPERATION" = "update" -a -e $AUTOUPDATEDIR/spec-patches-build/$JOB.spec.patch ]; then
      echo "Error: trying to make an update patch when a build patch already exists! Remove or create a build patch instead."
      exit 1
   fi

   diff $spec_dir/.$JOB.spec.autodist.pre$OPERATION $spec_dir/$JOB.spec > \
      $AUTOUPDATEDIR/spec-patches-$OPERATION/$JOB.spec.patch
   RET=$?

   if [ $RET -eq 1 ]; then
      echo "Created $OPERATION patch for $JOB:"
      cat $AUTOUPDATEDIR/spec-patches-$OPERATION/$JOB.spec.patch
      exit 0
   elif [ $RET -eq 0 ]; then
      echo "Info: files are identical."
      rm $AUTOUPDATEDIR/spec-patches-$OPERATION/$JOB.spec.patch
      exit 0
   else
      echo "Error: diff returned $RET while creating the patch."
      exit 1
   fi
elif [ "$COMMAND" = "schedule" ]; then
   [ "$JOB" ] || {
      echo "Scheduled jobs:"
      cat $AUTOUPDATEDIR/manual.in
      exit 0
   }
#   cat $BUILDDEPSFILE | sort -k2 -n |
#   while read line; do
#      set -- $line
#      if [ "`echo \" $1 \" | grep \" $JOB \"`" ]; then
   if [ "`grep \"^$JOB \" $SRCPKGLIST`" -o "`autodist --list-jobs | grep ^$JOB$`" ]; then
     echo "Job $JOB scheduled for automatic update or rebuild"
     sed -i "/^$JOB .*/d" $AUTOUPDATEDIR/manual.in
     echo "$JOB $JOBVER 0" >> $AUTOUPDATEDIR/manual.in || exit 1
   else
      echo "Unknown job $JOB."
   fi
#   done
elif [ "$COMMAND" = "unschedule" ]; then
   [ "$JOB" ] || {
      echo "Scheduled jobs:"
      cat $AUTOUPDATEDIR/manual.in
      exit 0
   }
#   cat $AUTOUPDATEDIR/manual.in | sort -k2 -n |
#   while read line; do
#      set -- $line
#      if [ "`echo \" $1 \" | grep \" $JOB \"`" ]; then
   if [ "`grep \"^$JOB \" $AUTOUPDATEDIR/manual.in`" ]; then
         sed -i "/^$JOB .*/d" $AUTOUPDATEDIR/manual.in
         echo "Job $JOB removed from schedule"
   fi
#   done
elif [ "$COMMAND" = "skip" ]; then
   [ "$JOB" ] || {
      echo "Error: package non specified."
      exit 1
   }
   SKIPPEDLINE=`awk '{ print $1" "$2 }' $SKIPPEDLISTDIR/*.skip | grep -m1 "^$JOB "`
   if [ "$SKIPPEDLINE" ]; then
      sed -i "/^$PKGNAME /d" $SKIPPEDLISTDIR/*.skip
   fi
   if [ "`grep \"^$JOB \" $SRCPKGLIST`" -o "`autodist --list-jobs | grep ^$JOB$`" ]; then
      echo "$JOB $DATE_NOW 101" >> $SKIPPEDLISTDIR/auto.skip
      echo "Job $JOB skipped from automatic builds"
   else
      echo "Unknown job $JOB."
   fi
else
   echo "Error: invalid command $COMMAND"
fi
