#!/bin/bash
# autoport -- batch cross-build tool based on autodist
# Copyright (C) 2011-2026 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
# Released under the terms of the GNU GPL release 3 license
#
. /usr/share/autodist/VERSION

BASE_REPOSITORY=base
PORT_REPOSITORY=base

. /etc/autodist/config

if [ -z "$DISTROQUERY_API_URL" ]; then
   echo "ERROR: DISTROQUERY_API_URL is not defined in configuration; aborting." >&2
   exit 1
fi

me=(${0##*/} $VERSION)

function usage() {
   echo "\
${me[0]} ${me[1]}
"$"Copyright (C) 2011-2025 Silvan Calarco <silvan.calarco@mambasoft.it>""
"$"Released under the terms of the GNU GPL v3 license"
   echo "
"$"Batch port and cross-build tool based on autodist.""

"$"Usage"":
   $me [-a][-f][-x arch1[,arch2],..][-r repository][-d release_repository][-s \"script_args\"]{--fix|job ...}

   -a                 "$"Automatic mode (use cache)
   -b                 "$"Batch port all packages in port repository to sync with base repository
   -f                 "$"Force operations
   -np                "$"Don't prepare jobs passed to command line unless specfile is not found
   -npa               "$"Don't prepare any jobs unless specfile is not found
   -nr                "$"Don't recurse build
   --fix              "$"Fix .la files for cross linking and exit
   -r                 "$"Work on given repository (default: $PORT_REPOSITORY)
   -d                 "$"Release packages to given repository (default: work repository)
   -h                 "$"Show this help and exit
   -s \"script_args\"   "$"Send script_args as arguments for autodist update-specfile script
   -u changelog       "$"Rebuild packages with given changelog
   -ud                "$"Rebuild dependent packages that need rebuild
   -v                 "$"More verbose output
   -x                 "$"Operate in cross build mode
   --user user        "$"Pass calling user name for Webbuild social messages

   arch               "$"Target architecture
   job                "$"Job specification for autodist""
"

}

function autoport_log() {
   local pkg=$1
   local operation=$2
   local result=$3
   local loggrep=`grep "?= See" $4 | sed "s|?= See ||"`
   local logfiles=""

   for f in $loggrep; do
      logfiles="$logfiles $f"
   done
   [ "$pkg" -a "$operation" -a "$result" -a "$BATCH_MODE" ] || return
   echo "$pkg $operation $result $logfiles" >> $DATADIR/autoport-$PORT_REPOSITORY-current
}

# for webbuild message
function cgi_encodevar() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""

  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}"    # You can either set a return variable (FASTER) 
#  REPLY="${encoded}"   #+or echo the result (EASIER)... or both... :p
}

function get_pkg_srcinfo() {

   local pkg rep _output _ret

   unset src_name src_version src_buildtime src_repository src_epoch src_release src_milestone

   [ $1 ] && rep=${1##*/} || exit 1
   [ $2 ] && pkg=$2 || return 1

   _output=$(autodist-distroquery repository-srclist "$rep" "$pkg" 2>&1)
   _ret=$?
   if [ $_ret -ne 0 ]; then
      echo "ERROR: autodist-distroquery failed (exit code $_ret): $(echo "$_output" | grep "^ERROR:" | head -1)" >&2
      return $_ret
   fi

   read src_name src_repository src_epoch src_version src_release src_buildtime \
      <<< "$(echo "$_output" | grep -v "^ERROR:" | head -1)"

   [ "$src_name" ] || return 3

   src_milestone=`echo $src_release | sed "s|[0-9.]*\(.*\)|\1|"`
}

# get_pkg_buildinfo - retrieve package metadata via the distroquery REST API.
#
# $1: repository data directory (repo name extracted from basename)
# $2: architecture
# $3: package name (optional; when omitted, populates pkg_list[] only)
function get_pkg_buildinfo() {

   local pkg local_arch rep _output _ret

   [ $1 ] && rep=${1##*/} || exit 1
   [ $2 ] && local_arch=$2 || exit 1
   [ $3 ] && pkg=$3

   if [ ! "$pkg" ]; then
      _output=$(autodist-distroquery repository "$rep" 2>&1)
      _ret=$?
      if [ $_ret -ne 0 ]; then
         echo "ERROR: autodist-distroquery failed (exit code $_ret): $(echo "$_output" | grep "^ERROR:" | head -1)" >&2
         return $_ret
      fi
      eval $(echo "$_output" | grep -v "^ERROR:")
      return 0
   fi

   _output=$(autodist-distroquery package "$rep" "$pkg" "$local_arch" 2>&1)
   _ret=$?
   if [ $_ret -ne 0 ]; then
      echo "ERROR: autodist-distroquery failed (exit code $_ret): $(echo "$_output" | grep "^ERROR:" | head -1)" >&2
      return $_ret
   fi
   eval $(echo "$_output" | grep -v "^ERROR:")

   [ "$pkg_name" ] || { unset pkg_name pkg_arch pkg_version pkg_release \
      pkg_group pkg_license pkg_size pkg_buildtime pkg_altrep pkg_repository; return 1; }

   _output=$(autodist-distroquery package-needrebuild "$rep" "$pkg" "$local_arch" 2>&1)
   _ret=$?
   if [ $_ret -ne 0 ]; then
      echo "ERROR: autodist-distroquery failed (exit code $_ret): $(echo "$_output" | grep "^ERROR:" | head -1)" >&2
      return $_ret
   fi
   eval $(echo "$_output" | grep -v "^ERROR:")
   return 0
}

function write_last_file() {
   [ "$AUTO_MODE" == "1" ] && LASTFILE=$DATADIR/last || LASTFILE=$DATADIR/lastmanual
   cat > $LASTFILE << _EOF
JOB_CANTPREPARE=(${JOB_CANTPREPARE[*]})
JOB_MISSINGREQ=(${JOB_MISSINGREQ[*]})
JOB_SENT=(${JOB_SENT[*]})
JOB_CANTINSTAL=(${JOB_CANTINSTALL[*]})
JOB_COMPLETED=(${JOB_COMPLETED[*]})
JOB_FAILED=(${JOB_FAILED[*]})
_EOF
}

function find_source_by_provide() {
   autodist-distroquery provide-sources "$1" "$2"
}

function find_requirements() {
   [ "$1" ] || return 1
   local tf=`mktemp` || {
      echo "Error: cannot create tempfile"
      return 1
   }
   local tf1=`mktemp`
   local OUTFILE=$1

   if [ "`grep " is needed by " $OUTFILE`" ]; then
      grep " is needed by " $OUTFILE | awk '{ print $1 }' | tr -d \' > $tf1
   elif [ "`grep " matches no packages" $OUTFILE`" ]; then
      grep " matches no packages" $OUTFILE | awk '{ print $2 }' | tr -d \' | sed "s|@.*||" > $tf1
   elif [ "`grep " no package provides " $OUTFILE`" ]; then
      grep " no package provides " $OUTFILE | sed "s|.* no package provides ||" > $tf1
   fi

   [ `stat -c %s  $tf1` -gt 0 ] && {
      echo -n " | missing requirements: "
      > $tf
      sort -u $tf1 | while read line; do
         REQ=`find_source_by_provide $PORT_REPOSITORY ${line}`
         if [ "$REQ" ]; then
            get_pkg_buildinfo $DATADIR/$PORT_REPOSITORY $TARGET_ARCH $REQ
            if [ "$pkg_name" ]; then
               echo -n "$line(notrebuilding) "
            else
               echo "${REQ}" >> $tf
            fi
         else
            echo -n "$line(unresolved) "
         fi
      done
      > $tf1
      cat $tf | sort -u 2>/dev/null | while read line; do
         if [ "`echo " ${JOB[*]} ${JOB_MISSINGREQ[*]} ${JOB_CANTINSTALL[*]} " | grep " $line "`" ]; then
            echo -n "($line) "
         else
            echo -n "$line "
            echo -n "$line " >> $tf1
         fi
      done
      JOB_MISSINGREQ=(${JOB_MISSINGREQ[*]} `cat $tf1`)
      echo
   }
   rm -f $tf $tf1
   return 0
}

fetch_repfiles() {
   local u

   for u in ${AUTOPORT_UNSTAGE_ARCHS}; do
      [ "$u" = "$TARGET_ARCH" ] && {
         rm -f $DATADIR/$PORT_REPOSITORY/unstage.$TARGET_ARCH
         return
      }
   done
   [ "$VERBOSE_MODE" = "1" ] && echo "Fetching ${REPS_BASE_URL}/${BASE_REPOSITORY}/unstage.$TARGET_ARCH"
   curl -f -L -s ${REPS_BASE_URL}/${BASE_REPOSITORY}/unstage.$TARGET_ARCH -o $DATADIR/$PORT_REPOSITORY/unstage.$TARGET_ARCH ||
      echo "Warning: unable to fetch ${REPS_BASE_URL}/${BASE_REPOSITORY}/unstage.$TARGET_ARCH"
}

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

[ "$BUILD_ARCH" ] || BUILD_ARCH=`uname -m`

case $BUILD_ARCH in
   i386|i486|i686) BUILD_ARCH=i586 ;;
   arm*) BUILD_ARCH=arm ;;
esac

TARGET_ARCHS=$BUILD_ARCH
JOB_CMDLINE=()
CROSS_MODE=

for ((i=1; i<=$#; i++)); do
   case ${!i} in
      -a) AUTO_MODE=1 ;;
      -b) BATCH_MODE=1 ;;
      -f) FORCE_MODE=1 ;;
      -v) VERBOSE_MODE=1 ;;
      -u) REBUILD_MODE=1
          shift
          REBUILD_CHANGELOG="${!i}"
          [ "$REBUILD_CHANGELOG" ] || {
             echo "ERROR: changelog not given as -u option parameter; aborting."
             exit 1
          }
          ;;
      -ud) REBUILD_DEPENDANT_MODE=1
           DONT_RECURSE=1
           ;;
      -npa) DONT_PREPARE_ANY=1
            DONT_PREPARE=1 ;;
      -np) DONT_PREPARE=1 ;;
      -nr) DONT_RECURSE=1 ;;
      --fix) FIX_MODE=1 ;;
      -r) shift
          PORT_REPOSITORY="${!i}"
          ;;
      -s) shift
          SCRIPT_ARGS="${!i}"
          ;;
      -d) shift
          DEST_REPOSITORY="${!i}"
          ;;
      -x) shift
          TARGET_ARCHS="`echo ${!i} | tr , ' '`"
          CROSS_MODE=1
          ;;
      --user) let i+=1
          [ "${!i}" ] || {
             echo "Error: --user parameter not given, aborting."
             exit 1
          }
          WEBBUILD_USER_CMDLINE="${!i}"
          ;;
      -h|--help)
          usage
          exit 0
          ;;
      *) JOB_CMDLINE=(${JOB_CMDLINE[*]} ${!i})
         ;;
   esac
done

[ "$DEST_REPOSITORY" ] || DEST_REPOSITORY=$PORT_REPOSITORY

if [ "$BATCH_MODE" ]; then
  PIDFILE="/run/autodist/autoport-${BUILD_ARCH}-${PORT_REPOSITORY}.pid"
  [ -e $PIDFILE ] && {
     PIDCHECK=`cat $PIDFILE`
     [ "$PIDCHECK" -a -e /proc/$PIDCHECK -a ! "$DISABLE_PID_CHECK" ] && {
        echo "Error: autoport -b already running with PID=$PIDCHECK.Aborting."
        exit 1
     }
  }
  echo $$ > $PIDFILE
  trap "rm -f $PIDFILE; exit 1" SIGQUIT SIGINT SIGKILL SIGTERM
fi

for TARGET_ARCH in ${TARGET_ARCHS}; do

   JOBS_SKIPPED=()
   JOBS_CANTPREPARE=()
   JOBS_CANTINSTALL=()
   JOB_MISSINGREQ=()
   JOB_COMPLETED=()
   JOB_FAILED=()

   DATADIR=$HOME/.autoport
   DATAARCHDIR=$DATADIR/$TARGET_ARCH
   DISTROMATIC_BASE_URL=${REPS_BASE_URL}/distromatic/
   BASE_REPOSITORY_DISTROMATIC_URL=${DISTROMATIC_BASE_URL}$BASE_REPOSITORY
   PORT_REPOSITORY_DISTROMATIC_URL=${DISTROMATIC_BASE_URL}$PORT_REPOSITORY

   [ -d $DATAARCHDIR ] || mkdir -p $DATAARCHDIR
   [ -d $DATADIR/$BASE_REPOSITORY ] || mkdir -p $DATADIR/$BASE_REPOSITORY
   [ -d $DATADIR/$PORT_REPOSITORY ] || mkdir -p $DATADIR/$PORT_REPOSITORY

   logdate=`date +"%F@%T"`
   logfile=$DATAARCHDIR/$logdate.log
   [ -e $DATAARCHDIR/$PORT_REPOSITORY-current.log ] && mv $DATAARCHDIR/$PORT_REPOSITORY-current.log $DATAARCHDIR/$PORT_REPOSITORY-last.log
   > $logfile
   ln -sf $logdate.log $DATAARCHDIR/$PORT_REPOSITORY-current.log

   BUILD_PLATFORM=`rpm --target $TARGET_ARCH --eval %{_build}`
   BUILD_CPU=`echo $BUILD_PLATFORM | cut -d- -f1`
   TARGET_PLATFORM=`rpm --target $TARGET_ARCH --eval %{_target_platform}`
   TARGET_CPU=`echo $TARGET_PLATFORM | cut -d- -f1`
   eval `autospec --eval spec_dir | tail -n1`
   if [ "$spec_dir" ]; then
      SPECDIR=$spec_dir
   else
      SPECDIR=`rpm --eval %{_specdir}`
   fi

   echo "Build platform is $BUILD_PLATFORM"
   echo "Target platform is $TARGET_PLATFORM"
   echo "Port repository is $PORT_REPOSITORY"
   echo "Release repository is $DEST_REPOSITORY"

   fetch_repfiles

   [ "$AUTO_MODE" -a -e $DATAARCHDIR/last ] && . $DATAARCHDIR/last
   if [ "$REBUILD_DEPENDANT_MODE" ]; then
      for j in ${JOB_CMDLINE[*]}; do
         get_pkg_buildinfo $DATADIR/$PORT_REPOSITORY $TARGET_ARCH $j
         port_pkg_list=${pkg_needrebuild[*]}
         port_pkg_builds=" ${pkg_builds[*]} "
         port_pkg_epoch=$pkg_epoch
         port_pkg_version=$pkg_version
         port_pkg_release=$pkg_release
         currrebuild=
         currjobscriptargs=
         JOBNUM=0
         for p in ${port_pkg_list}; do
            if [ "${p/@*}" != "$currrebuild" ]; then
               [ $JOBNUM -gt 0 ] && JOB_SCRIPTARGS[$JOBNUM-1]="$currjobscriptargs"
               currjobscriptargs=
               currrebuild=${p/@*}
               get_pkg_buildinfo $DATADIR/$PORT_REPOSITORY $TARGET_ARCH $currrebuild
               for ignore_pkg in ${AUTOPORT_IGNORE_LIST}; do
                  [ "${currrebuild}" == "${ignore_pkg}" ] && continue
               done
               JOB=(${JOB[*]} $currrebuild)
               let JOBNUM+=1
            fi
            currprovider=${p/*@}
            if [ "${currprovider/-devel}" == "$currprovider" ]; then
               # Prefer the -devel subpackage as BuildRequires target, but only if it actually
               # exists in the repository. Some packages (e.g. ocaml-compiler-libs,
               # ocaml-runtime) have no -devel variant; adding a non-existent -devel name as
               # BuildRequires would cause every dependent to fail with unresolved requirements.
               _devel_candidate="${currprovider}-devel"
               _devel_name=$(autodist-distroquery package "$PORT_REPOSITORY" "$_devel_candidate" "$TARGET_ARCH" 2>/dev/null | \
                             grep "^pkg_name=" | tr -d "'\"" | sed "s/^pkg_name=//")
               [ -n "$_devel_name" ] && currprovider="$_devel_candidate"
            fi
            add_epoch=""
            [ "${port_pkg_epoch}" ] && add_epoch="${port_pkg_epoch}:"
            currjobscriptargs="$currjobscriptargs --buildreq ${currprovider} >= ${add_epoch}${port_pkg_version}-${port_pkg_release} "
         done
         [ "$currjobscriptargs" ] && JOB_SCRIPTARGS[$JOBNUM-1]="$currjobscriptargs"
         echo "Package dependant from $j to rebuild: ${JOB[*]}"
      done
   else
      JOB=(${JOB_CMDLINE[*]} ${JOB_MISSINGREQ[*]} ${JOB_FAILED[*]})
   fi
   [ "$FIX_MODE" -o "$BATCH_MODE" -o "${JOB[*]}" ] || { echo "Nothing to do."; exit 0; }

   if [ "$BATCH_MODE" ]; then

      [ "${JOB[*]}" ] && {
         echo "Error: no jobs allowed in batch mode; aborting."
         exit 1
      }
      get_pkg_buildinfo $DATADIR/$PORT_REPOSITORY $TARGET_ARCH
      port_pkg_list=${pkg_list[*]}

      if [ "$REBUILD_MILESTONE" ]; then
         for p in ${port_pkg_list}; do
            get_pkg_buildinfo $DATADIR/$PORT_REPOSITORY $TARGET_ARCH $p
            target_pkg_name=$pkg_name
            target_pkg_ver=$pkg_version
            target_pkg_rel=$pkg_release
            target_pkg_milestone=`echo $target_pkg_rel | sed "s|[0-9.]*\(.*\)|\1|"`
            if [ "$target_pkg_milestone" != "$REBUILD_MILESTONE" ]; then
               JOB=(${JOB[*]} $p)
            fi
         done
         echo "Packages to rebuild: ${JOB[*]}"
      fi
      _output=$(autodist-distroquery repository-problems "$PORT_REPOSITORY" "$TARGET_ARCH" 2>&1)
      if [ $? -ne 0 ]; then
         echo "ERROR: autodist-distroquery failed: $(echo "$_output" | grep "^ERROR:" | head -1)" >&2
      else
         eval $(echo "$_output" | grep -v "^ERROR:")
      fi
      echo "Packages to port: ${needport_list[*]}"
      for p in ${needport_list[*]}; do
         JOB=(${JOB[*]} $p)
      done

      [ -e $DATADIR/autoport-$PORT_REPOSITORY-current ] && \
          mv $DATADIR/autoport-$PORT_REPOSITORY-current $DATADIR/autoport-$PORT_REPOSITORY-last
   fi

   step=0
   tmpfile=`mktemp`
   tmpfile1=`mktemp`

   [ -e $DATAARCHDIR/sentjobs ] || > $DATAARCHDIR/sentjobs
   find $DATAARCHDIR/sentjobs -cmin 120 >/dev/null && {
      > $DATAARCHDIR/sentjobs
   }

   [ -e $DATAARCHDIR/preparedjobs ] || > $DATAARCHDIR/preparedjobs
   find $DATAARCHDIR/preparedjobs -ctime 1 >/dev/null && {
      > $DATAARCHDIR/preparedjobs
   }

   [ -e $DATAARCHDIR/updatedjobs ] || > $DATAARCHDIR/updatedjobs
   find $DATAARCHDIR/updatedjobs -ctime 1 >/dev/null && {
      > $DATAARCHDIR/updatedjobs
   }

   [ "$FIX_MODE" ] && exit 0

   [ "$FORCE_MODE" ] && FORCE_FLAG="--force"

  while [ "${JOB[*]}" ]; do
      step=`expr $step + 1`
      [ $step -gt 1 ] && fetch_repfiles
      [ "$BATCH_MODE" -a $step -gt $AUTOPORT_MAX_STEPS ] && {
         echo "Maximum steps count reached for batch mode; exiting. See $logfile for details."
         break
      }
      echo "=========="
      echo "# Step $step"
      echo "> Scheduled jobs: ${JOB[*]}"
      echo
      JOB_MISSINGREQ_OLD=(${JOB_MISSINGREQ[*]})
      JOB_MISSINGREQ=()
      JOB_MAYBEINSTALLED_NEW=()
      JOB_FAILED_OLD=(${JOB_FAILED[*]})
      JOB_FAILED=()
      JOB_CANTINSTALL_OLD=(${JOB_CANTINSTALL[*]})
      for JOB_NUM in `seq 1 ${#JOB[*]}`; do
         JOB_CURRENT=${JOB[$JOB_NUM-1]/\#*}
         if [ "${JOB_CURRENT}" != "${JOB[$JOB_NUM-1]}" ]; then
            JOB_CURRENT_VERSION=${JOB[$JOB_NUM-1]/*\#}
         else
            JOB_CURRENT_VERSION=
         fi
         if [ ! -s $DATADIR/$PORT_REPOSITORY/unstage.${TARGET_ARCH} -o \
              "`grep "^${JOB_CURRENT}$" /etc/autodist/unstage.${TARGET_ARCH} 2>/dev/null`" -o \
              "`grep "^${JOB_CURRENT}$" $DATADIR/$PORT_REPOSITORY/unstage.${TARGET_ARCH} 2>/dev/null`" ]; then
            echo -n "^ $JOB_CURRENT: "
            STAGEOPTS=
         else
            echo -n "^ $JOB_CURRENT(staging): "
            STAGEOPTS="$AUTOPORT_STAGEVARS"
         fi
         if [ "$CROSS_MODE" -a "${JOB_CURRENT:0:5}" == "perl-" ]; then
            echo "< (SKIPPED) [unable to cross-build perl modules]"
            JOB_SKIPPED=(${JOB_SKIPPED[*]} $JOB_CURRENT)
            continue
         fi
         [ "`grep ^$JOB_CURRENT\$ $DATAARCHDIR/sentjobs`" -a ! "$FORCE_MODE" ] && {
            echo "< (SKIPPED) [already sent]"
            JOB_SKIPPED=(${JOB_SKIPPED[*]} $JOB_CURRENT)
            continue
         }
         if [ "${JOB_MAYBEINSTALLED_NEW[*]}" = "" -a \
              "${JOB_MISSINGREQ_OLD[*]}" != "" -a \
              "`echo "( ${JOB_FAILED_OLD[*]} )"| grep " $JOB_CURRENT "`" ]; then
            echo "< (FAILED) [nothing new since previous attempt]"
            JOB_FAILED=(${JOB_FAILED[*]} $JOB_CURRENT)
            continue
         fi
         get_pkg_srcinfo $DATADIR/$PORT_REPOSITORY ${JOB_CURRENT} || {
            echo "< (FAILED) [error $? in get_pkg_srcinfo]"
            JOB_FAILED=(${JOB_FAILED[*]} $JOB_CURRENT)
            continue
         }
         if [ "$JOB_CURRENT_VERSION" -a \
              "$JOB_CURRENT_VERSION" != "$src_version" -a \
              "$JOB_CURRENT_VERSION" != "$src_version-$src_release" ]; then
            echo "< (FAILED) [wrong version $src_version-$src_release != $JOB_CURRENT_VERSION]"
            JOB_FAILED=(${JOB_FAILED[*]} $JOB_CURRENT)
            continue
         fi

         DONT_PREPARE_THIS=
         RELEASE_UPDATED=
         grep -q ^$JOB_CURRENT\$ $DATAARCHDIR/preparedjobs && RELEASE_UPDATED=1
         for p in ${JOB_CMDLINE[*]}; do
            [ "$p" = "${JOB_CURRENT}" -o "$DONT_PREPARE_ANY" ] || continue
            [ -e ${SPECDIR}/${JOB_CURRENT}.spec ] && DONT_PREPARE_THIS="$DONT_PREPARE"
         done
         [ ! "$DONT_PREPARE_THIS" -a ! "`grep ^$JOB_CURRENT\$ $DATAARCHDIR/preparedjobs`" ] && {
            echo -n "prepare"
            if [ ! "$REBUILD_DEPENDANT_MODE" -a "$step" = "1" ]; then
               REPOSITORY_STRICT_ADD="--repository-strict"
            else
               REPOSITORY_STRICT_ADD=""
            fi
#            [ "$VERBOSE_MODE" ] && echo "
#%% COMMAND: LANG=C LC_ALL=C autodist -p ${JOB_CURRENT} --server $PORT_REPOSITORY --repository $PORT_REPOSITORY $REPOSITORY_STRICT_ADD --severity 2 -- $STAGEOPTS"
            LANG=C LC_ALL=C autodist -p ${JOB_CURRENT} --server $PORT_REPOSITORY --repository $PORT_REPOSITORY $REPOSITORY_STRICT_ADD --severity 2 -- $STAGEOPTS &>$tmpfile
            [ $? -gt 0 ] && {
               echo "(FAILED) "
               JOB_CANTPREPARE=(${JOB_CANTPREPARE[*]} $JOB_CURRENT)
               autoport_log ${JOB_CURRENT} prepare failed $tmpfile
               continue
            } || {
               spec_version=`grep -m1 "^Version:" ${SPECDIR}/${JOB_CURRENT}.spec | awk '{ print $2}'`
               spec_release=`grep -m1 "^Release:" ${SPECDIR}/${JOB_CURRENT}.spec | awk '{ print $2}'`
               if [ "${spec_version}-${spec_release}" != "$src_version-$src_release" ]; then
                  echo "< (FAILED) [wrong version ${src_version}-${src_release} != ${spec_version}-${spec_release}]"
                  JOB_CANTPREPARE=(${JOB_CANTPREPARE[*]} $JOB_CURRENT)
                  autoport_log ${JOB_CURRENT} prepare failed $tmpfile
                  continue
               fi
               echo $JOB_CURRENT >> $DATAARCHDIR/preparedjobs
               echo -n "(OK) "
               autoport_log ${JOB_CURRENT} prepare ok $tmpfile
            }
            if [ "$REBUILD_MODE" = "1" -o "$REBUILD_DEPENDANT_MODE" = "1" ]; then
               echo -n "update"
               if [ "$REBUILD_DEPENDANT_MODE" ]; then
                  SCRIPT_ARGS="${JOB_SCRIPTARGS[$JOB_NUM-1]}"
                  changelogadd="`echo ${SCRIPT_ARGS} | sed "s|--buildreq|,|g;s| ||g;s|^,||g"`"
                  REBUILD_CHANGELOG="rebuilt by autoport with build requirements: '${changelogadd}'"
               fi
#               [ "$VERBOSE_MODE" ] && echo "
#%% COMMAND: LANG=C LC_ALL=C autodist -u --rebuild -r ${JOB_CURRENT} --severity 2 -- $STAGEOPTS --changelog \"$REBUILD_CHANGELOG\" --- \"$SCRIPT_ARGS\""
               LANG=C LC_ALL=C autodist -u --rebuild -r ${JOB_CURRENT} --severity 2 -- $STAGEOPTS --changelog \"$REBUILD_CHANGELOG\" --- $SCRIPT_ARGS &>$tmpfile
               [ $? -gt 0 ] && {
                  echo "(FAILED) "
                  autoport_log ${JOB_CURRENT} update failed $tmpfile
                  continue
               } || {
                  autoport_log ${JOB_CURRENT} update ok $tmpfile
                  echo -n "(OK) "
                  RELEASE_UPDATED=1
                  echo $JOB_CURRENT >> $DATAARCHDIR/updatedjobs
               }
            elif [ "$PORT_REPOSITORY" != "$DEST_REPOSITORY" ]; then
               echo -n "update"
#               [ "$VERBOSE_MODE" ] && echo "
#%% COMMAND: LANG=C LC_ALL=C autodist -u -r ${JOB_CURRENT} --severity 2 -- $STAGEOPTS --changelog \"automatic port from $PORT_REPOSITORY\""
               LANG=C LC_ALL=C autodist -u -r ${JOB_CURRENT} --severity 2 -- $STAGEOPTS --changelog \"automatic port from $PORT_REPOSITORY\" --- $SCRIPT_ARGS &>$tmpfile
               cat $tmpfile >> $logfile
               [ $? -gt 0 ] && {
                  echo "(FAILED) "
                  autoport_log ${JOB_CURRENT} update failed $tmpfile
                  continue
               } || {
                  autoport_log ${JOB_CURRENT} update ok $tmpfile
                  echo -n "(OK) "
                  RELEASE_UPDATED=1
               }
            elif [ "$REBUILD_MILESTONE" -a "$src_milestone" != "$REBUILD_MILESTONE" ]; then
               echo -n "update"
               [ "$src_repository" -a "$src_version" -a "$src_release" ] || {
                  echo "(FAILED) [can't get repository information for this package]"
                  continue
               }
               src_release_num=`echo $src_release | sed "s|\([0-9.]*\).*|\1|"`
#               [ "$VERBOSE_MODE" ] && echo "
#%% COMMAND: LANG=C LC_ALL=C autospec -u ${JOB_CURRENT} -a4 --changelog \"$REBUILD_MILESTONE milestone rebuild\" $src_version $src_release_num${REBUILD_MILESTONE}"
               # FIXME: should be implemented in autodist
               LANG=C LC_ALL=C autospec -u ${JOB_CURRENT} -a4 --changelog "$REBUILD_MILESTONE milestone rebuild" $src_version $src_release_num${REBUILD_MILESTONE} &>$tmpfile
               [ $? -gt 0 ] && {
                  if [ "$VERBOSE_MODE" ]; then
                     echo
                     echo "= Failed output:"
                     echo "================"
                     cat $tmpfile
                     echo "================"
                     echo
                  fi
                  cat $tmpfile >> $logfile
                  JOB_FAILED=(${JOB_FAILED[*]} $JOB_CURRENT)
                  echo "(FAILED)"
                  JOB_LOG=`grep "^?= See " $tmpfile | awk '{ print $3 }'`
                  [ "$JOB_LOG" ] || JOB_LOG=$tmpfile
                  autoport_log ${JOB_CURRENT} update failed $tmpfile
                  continue
               } || {
                  cat $tmpfile >> $logfile
                  echo -n "(OK) "
                  autoport_log ${JOB_CURRENT} update ok $tmpfile
                  RELEASE_UPDATED=1
               }
            fi
         }
         echo -n "port"
         if [ ! "$src_repository" -a "$DONT_PREPARE_THIS" != "1" ]; then
            echo "(FAILED) [can't get repository information for this package]"
            autoport_log ${JOB_CURRENT} port failed
            continue
         fi
         SEND_REPOSITORY=$DEST_REPOSITORY
         if [ ! "$RELEASE_UPDATED" -a ! "$DONT_PREPARE_THIS" ]; then
            SEND_REPOSITORY=$src_repository
            # Add --nosrpm if source is not modified
            NOSRPM_OPTS="--nosrpm"
         fi
         LANG=C LC_ALL=C autodist -b -s ${JOB_CURRENT} --arch $TARGET_ARCH --server $SEND_REPOSITORY --repository $PORT_REPOSITORY --severity 2 $FORCE_FLAG -- $STAGEOPTS $NOSRPM_OPTS &>$tmpfile
         [ $? -gt 0 ] && {
            if [ "$VERBOSE_MODE" ]; then
               echo
               echo "= Failed output:"
               echo "================"
               cat $tmpfile
               echo "================"
               echo
            fi
            cat $tmpfile >> $logfile
            JOB_FAILED=(${JOB_FAILED[*]} $JOB_CURRENT)
            echo "(FAILED)"
            JOB_LOG=`grep "^?= See " $tmpfile | awk '{ print $3 }'`
            [ "$JOB_LOG" ] || JOB_LOG=$tmpfile
            [ ! "$REBUILD_MILESTONE" ] && find_requirements $JOB_LOG
            autoport_log ${JOB_CURRENT} port failed $tmpfile
            continue
         } || {
            cat $tmpfile >> $logfile
            JOB_SENT=(${JOB_SENT[*]} $JOB_CURRENT)
            echo $JOB_CURRENT >> $DATAARCHDIR/sentjobs
            echo -n "(OK) "
            autoport_log ${JOB_CURRENT} port ok $tmpfile
            [ "$WEBBUILD_USER" -a "$DISTROQUERY_API_URL" ] && {
               if [ "$WEBBUILD_USER_CMDLINE" ]; then
                  autodist-distroquery social-log post "$WEBBUILD_USER" \
"(invoked by $WEBBUILD_USER_CMDLINE) ported <b>$JOB_CURRENT</b> to <b>$TARGET_ARCH</b> and sent it to <b>$SEND_REPOSITORY</b>" \
"" "" "false" "$WEBBUILD_EMAIL" 2>/dev/null
               else
                  autodist-distroquery social-log post "$WEBBUILD_USER" \
"ported <b>$JOB_CURRENT</b> to <b>$TARGET_ARCH</b> and sent it to <b>$SEND_REPOSITORY</b>" \
"" "" "false" "$WEBBUILD_EMAIL" 2>/dev/null
               fi
            }
         }
         echo -n "install"
         LANG=C LC_ALL=C autodist -i ${JOB_CURRENT} --arch $TARGET_ARCH --force --severity 2 -- $STAGEOPTS &>$tmpfile
         # --server $DEST_REPOSITORY --repository $PORT_REPOSITORY
         [ $? -gt 0 ] && {
            if [ "$VERBOSE_MODE" ]; then
               echo
               echo "= Failed output:"
               echo "================"
               cat $tmpfile
               echo "================"
               echo
            fi
            cat $tmpfile >> $logfile
            JOB_CANTINSTALL=(${JOB_CANTINSTALL[*]} $JOB_CURRENT)
            echo $JOB_CURRENT >> $DATAARCHDIR/checkinstalljobs
            echo "(FAILED)"

            JOB_MAYBEINSTALLED_NEW=(${JOB_MAYBEINSTALLED_NEW[*]} $JOB_CURRENT)
            JOB_LOG=`grep "^?= See " $tmpfile | awk '{ print $3 }'`
            [ "$JOB_LOG" ] || JOB_LOG=$tmpfile
            [ ! "$REBUILD_MILESTONE" ] && find_requirements $JOB_LOG
            autoport_log ${JOB_CURRENT} install failed $tmpfile
            continue
         } || {
            cat $tmpfile >> $logfile
            JOB_MAYBEINSTALLED_NEW=(${JOB_MAYBEINSTALLED_NEW[*]} $JOB_CURRENT)
            JOB_COMPLETED=(${JOB_COMPLETED[*]} $JOB_CURRENT)
            echo -n "(OK)"
            autoport_log ${JOB_CURRENT} install ok $tmpfile
         }
         echo
      done
      if [ "${JOB[*]}" == "${JOB_FAILED[*]}" -a "${JOB_MISSINGREQ[*]}" == "" ]; then
         # some new build, retry cantinstall jobs
         JOB_CANTINSTALL=()
         for JOB_NUM in `seq 1 ${#JOB_CANTINSTALL_OLD[*]}`; do
            JOB_CURRENT=${JOB_CANTINSTALL_OLD[$JOB_NUM-1]}
            if [ "`grep "^${JOB_CURRENT}$" /etc/autodist/unstage.${TARGET_ARCH} 2>/dev/null`" -o \
                 "`grep "^${JOB_CURRENT}$" $DATADIR/$PORT_REPOSITORY/unstage.$TARGET_ARCH 2>/dev/null`" ]; then
               echo -n "$JOB_CURRENT: "
               STAGEOPTS=
            else
               echo -n "$JOB_CURRENT(staging): "
               STAGEOPTS="$AUTOPORT_STAGEVARS"
            fi
            echo -n "retryinstall"
            LANG=C LC_ALL=C autodist -i ${JOB_CURRENT} --arch $TARGET_ARCH --force --severity 2 -- $STAGEOPTS &>>$tmpfile
            # --server $DEST_REPOSITORY --repository $PORT_REPOSITORY
            [ $? -gt 0 ] && {
               if [ "$VERBOSE_MODE" ]; then
                  echo
                  echo "= Failed output:"
                  echo "================"
                  cat $tmpfile
                  echo "================"
                  echo
               fi
               cat $tmpfile >> $logfile
               JOB_CANTINSTALL=(${JOB_CANTINSTALL[*]} $JOB_CURRENT)
               echo $JOB_CURRENT >> $DATAARCHDIR/checkinstalljobs
               echo "(FAILED)"

               JOB_MAYBEINSTALLED_NEW=(${JOB_MAYBEINSTALLED_NEW[*]} $JOB_CURRENT)
               JOB_LOG=`grep "^?= See " $tmpfile | awk '{ print $3 }'`
               [ "$JOB_LOG" ] || JOB_LOG=$tmpfile
               find_requirements $JOB_LOG
               continue
            } || {
               cat $tmpfile >> $logfile
               JOB_MAYBEINSTALLED_NEW=(${JOB_MAYBEINSTALLED_NEW[*]} $JOB_CURRENT)
               JOB_COMPLETED=(${JOB_COMPLETED[*]} $JOB_CURRENT)
               echo -n "(OK)"
               autoport_log ${JOB_CURRENT} retryinstall ok $tmpfile
            }
            echo
         done
      fi
      echo "| Skipped jobs: ${JOB_SKIPPED[*]}"
      echo "| Unpreparable jobs: ${JOB_CANTPREPARE[*]}"
      echo "| Missing requirements: ${JOB_MISSINGREQ[*]}"
      echo "| Completed jobs: ${JOB_COMPLETED[*]}"
      echo "| Installation error for: ${JOB_CANTINSTALL[*]}"
      echo "| Failed jobs: ${JOB_FAILED[*]}"
      write_last_file

      [ "${JOB[*]}" == "${JOB_FAILED[*]}" -a \
        "${JOB_MISSINGREQ[*]}" == "" -a \
        "${JOB_CANTINSTALL_OLD[*]}" == "${JOB_CANTINSTALL[*]}"  ] && {
         echo "! Loop detected. Finishing. See $logfile for details."
         rm -f $tmpfile $tmpfile1
         exit 1
      }
      [ "$DONT_RECURSE" ] && break
      JOB=(${JOB_MISSINGREQ[*]} ${JOB_FAILED[*]})
   done
   rm -f $tmpfile $tmpfile1
done
echo "* All jobs completed. See $logfile for details."
[ "$BATCH_MODE" ] && rm -f $PIDFILE
exit ${#JOB_FAILED[*]}
