#!/bin/bash
#
# autoport launcher
# (c) 2021-2024 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
#. /etc/sysconfig/openmamba-central
. /etc/sysconfig/autoport

[ "$AUTOPORT_ENABLE" = "1" -o "$AUTOPORT_ENABLE" = "true" ] || exit 0

i=$1

[ "$i" ] || {
   echo "Usage: autoport-launcher autoport_conf"
   exit 1
}

[ "`uname -m`" = "x86_64" ] && HOST_IS_X86_64=1

autodist-cleanold

[ "${AUTOPORT_DISABLE[$i]}" -a "${AUTOPORT_DISABLE[$i]}" != "0" ] && exit 0

PIDFILE=/run/autodist/autoport-launcher-${i}.pid
[ -e $PIDFILE ] && {
   PIDCHECK=`cat $PIDFILE`
   [ "$PIDCHECK" -a -e /proc/$PIDCHECK ] && {
      echo "autoport-launcher for configuration $i already running; exiting."
      exit 0
   }
}
echo $$ > $PIDFILE
trap "rm -f $PIDFILE; exit 1" SIGQUIT SIGINT SIGKILL SIGTERM

if [ "$HOST_IS_X86_64" -a "${AUTOPORT_ARCH[$i]}" != "x86_64" ]; then
   CMD_PREFIX=linux32
else
   CMD_PREFIX=
fi

UPDATE_DONE=

for r in ${AUTOPORT_REPOSITORIES[$i]}; do

   if [ "${AUTOPORT_NATIVE[$i]}" ]; then
      AUTOPORT_PIDFILE="/run/autodist/autoport-${AUTOPORT_ARCH[$i]}-$r.pid"
      LOGFILE="/var/autodist/log/autoport-native-$r-${AUTOPORT_ARCH[$i]}.log"
      LASTLOGFILE="/var/autodist/log/autoport-native-$r-${AUTOPORT_ARCH[$i]}-last.log"
   elif [ "${AUTOPORT_CHROOT[$i]}" ]; then
      AUTOPORT_PIDFILE="/var/autoport/${AUTOPORT_CHROOT[$i]}/run/autodist/autoport-${AUTOPORT_ARCH[$i]}-$r.pid"
      LOGFILE="/var/autodist/log/autoport-chroot-$r-${AUTOPORT_CHROOT[$i]}.log"
      LASTLOGFILE="/var/autodist/log/autoport-chroot-$r-${AUTOPORT_CHROOT[$i]}-last.log"
   elif [ "${AUTOPORT_CROSS[$i]}" ]; then
      AUTOPORT_PIDFILE="/run/autoport/autoport-${AUTOPORT_ARCH[$i]}-$r.pid"
      LOGFILE="/var/autodist/log/autoport-cross-$r-${AUTOPORT_CROSS[$i]}.log"
      LASTLOGFILE="/var/autodist/log/autoport-cross-$r-${AUTOPORT_CROSS[$i]}-last.log"
   else
      continue
   fi

   if [ "$UPDATE_DONE" == "" ]; then
      if [ "${AUTOPORT_NATIVE[$i]}" ]; then
         [[ "`/usr/bin/tty`" != "not a tty" ]] && echo "Updating packages in ${AUTOPORT_ARCH[$i]} native environment"
         echo "= Updating packages in ${AUTOPORT_ARCH[$i]} native environment..." >> $LOGFILE
         LANG=C dnf refresh >> $LOGFILE
         LANG=C dnf update -y >> $LOGFILE
         UPDATE_DONE=1
      elif [ "${AUTOPORT_CHROOT[$i]}" ]; then
         [[ "`/usr/bin/tty`" != "not a tty" ]] && echo "Updating packages in ${AUTOPORT_CHROOT[$i]} chroot environment"
         #echo "= Updating packages in ${AUTOPORT_CHROOT[$i]} chroot environment..." >> $LOGFILE
         LANG=C /usr/sbin/chroot /var/autoport/${AUTOPORT_CHROOT[$i]} dnf refresh > /dev/null
         LANG=C /usr/sbin/chroot /var/autoport/${AUTOPORT_CHROOT[$i]} dnf update -y > /dev/null
         #[ "${AUTOPORT_CHROOT_ICECREAM[$i]}" ] && LANG=C /usr/sbin/chroot /var/autoport/${AUTOPORT_CHROOT[$i]} /etc/init.d/icecream start
         UPDATE_DONE=1
      fi
   fi

   if [ -e $AUTOPORT_PIDFILE ]; then
      PIDCHECK=`cat $AUTOPORT_PIDFILE`
      if [ "$PIDCHECK" -a -e /proc/$PIDCHECK ]; then
         echo "autoport for configuration $i already running; continuing."
         continue
      fi
   fi

   [ -e $LOGFILE ] && mv $LOGFILE $LASTLOGFILE

   echo "+ Autoport start at `date`" > $LOGFILE
   echo "============================================" >> $LOGFILE

   if [ "${AUTOPORT_NATIVE[$i]}" ]; then
      echo "= Working on $r(${AUTOPORT_ARCH[$i]}) in native mode" >> $LOGFILE
      su -l ${AUTOPORT_CHROOT_USER[$i]} -c "$CMD_PREFIX autoport -b -r $r" >> $LOGFILE
      AUTODIST_HOME=`getent passwd ${AUTOPORT_CHROOT_USER[$i]} | cut -d: -f6`
      cat $AUTODIST_HOME/.autoport/${AUTOPORT_ARCH[$i]}/$r-current.log >> $LOGFILE
   elif [ "${AUTOPORT_CHROOT[$i]}" ]; then
      echo "= Working on $r(${AUTOPORT_CHROOT[$i]}) in chroot mode..." >> $LOGFILE
      $CMD_PREFIX /usr/sbin/chroot /var/autoport/${AUTOPORT_CHROOT[$i]} su -l ${AUTOPORT_CHROOT_USER[$i]} -c "autoport -b -r $r" >> $LOGFILE
      if [ "${AUTOPORT_CHROOT_USER[$i]}" = "autodist" ]; then
         cat /var/autoport/${AUTOPORT_CHROOT[$i]}/var/autodist/.autoport/${AUTOPORT_ARCH[$i]}/$r-current.log >> $LOGFILE
      else
         cat /var/autoport/${AUTOPORT_CHROOT[$i]}/home/${AUTOPORT_CHROOT_USER[$i]}/.autoport/${AUTOPORT_ARCH[$i]}/$r-current.log >> $LOGFILE
      fi
   elif [ "${AUTOPORT_CROSS[$i]}" ]; then
      [[ "`/usr/bin/tty`" != "not a tty" ]] && echo "Working on $r(${AUTOPORT_CROSS[$i]}) in cross-platform mode"
      echo "= Working on $r(${AUTOPORT_CROSS[$i]}) in cross-platform mode" >> $LOGFILE
      su -l autodist -c "autoport -b -x ${AUTOPORT_CROSS[$i]} -r $r" >> $LOGFILE
      cat /var/autodist/.autoport/${AUTOPORT_CROSS[$i]}/$r-current.log >> $LOGFILE
   fi

   echo "- Autoport end at `date`" >> $LOGFILE
   echo "============================================" >> $LOGFILE

done
rm -f ${PIDFILE}
