#!/bin/bash

# this 'computes' WAIT4DEB_URLS and writes it to the env file specifed as $1 (default: upstream.env)

FIRSTCHANGELOG=$(ls -1d debian-*/changelog |head -1)

if [ -z "${FIRSTCHANGELOG}" ] ; then
  echo "ERROR: Unable to find a debian-*/changelog"
  exit 1
fi
export DEBVER=$(head -1 ${FIRSTCHANGELOG} |sed -e 's/^.*(//' |sed -e 's/).*$//')

DEBCONTROLS=$(ls -1d debian-*/control)

if [ -z "${DEBCONTROLS}" ] ; then
  echo "ERROR: Unable to find debian-*/control"
  exit 2
fi
NR_DEBCONTROLS=$(echo ${DEBCONTROLS} |wc -w |tr -d ' ')
echo "GOOD: Found ${NR_DEBCONTROLS} debian-*/control to process." 

export UPSTREAMENV="$1"
if [ "${UPSTREAMENV}" = "" ] ; then
  UPSTREAMENV="upstream.env"
fi

export WAIT4DEB_URLS=""
export WAIT4DEB_PBAGENS=""

for DEBCONTROL in ${DEBCONTROLS} ; do
  echo "INFO: Parsing ${DEBCONTROL} ..."
  PBAREPOS=$(egrep "^XBCS-PBA-Repositories: " ${DEBCONTROL} |cut -f2- -d' ' |head -1)
  for PBAREPO in ${PBAREPOS} ; do
    echo "INFO: Processing ${DEBCONTROL} repo ${PBAREPO} ..."
    PBADISTROS=$(egrep "^XBCS-PBA-Distributions: " ${DEBCONTROL} |cut -f2- -d' ' |head -1)
    test -z "${PBADISTROS}" && PBADISTROS=$(egrep "^XBCS-PBA-Distribution: " ${DEBCONTROL} |cut -f2 -d' ' |head -1)
    for PBADISTRO in ${PBADISTROS} ; do
      echo "INFO: Processing ${DEBCONTROL} repo ${PBAREPO} distro ${PBADISTRO} ..."
      
      PBADISTFAMILY="debian"
      if [ "${PBADISTRO}" = "any" ] ; then
        PBADISTFAMILY="any"
      fi
      
      PBACAT=$(egrep "^XBCS-PBA-Category: " ${DEBCONTROL} |cut -f2 -d' ' |head -1)
      
      PBAGEN=$(egrep "^XBCS-PBA-Generation: " ${DEBCONTROL} |cut -f2 -d' ' |head -1)
      BETAVER=$(echo "${DEBVER}" |grep beta)
      test -n "${BETAVER}" && PBAGEN="${PBAGEN}beta"
      
      if [ -z "${WAIT4DEB_PBAGENS}" ] ; then
        WAIT4DEB_PBAGENS="${PBAGEN}"
      else
        WAIT4DEB_PBAGENS="${WAIT4DEB_PBAGENS} ${PBAGEN}"
      fi
    
      PBAPROTOCOL="https"
      echo "INFO: Testing if repo supports https by checking https://${PBAREPO}/ ..."
      wget --spider https://${PBAREPO}/ >/dev/null 2>/dev/null || PBAPROTOCOL="http"
      echo "INFO: Testing repo reachbility by checking ${PBAPROTOCOL}://${PBAREPO}/ ..."
      wget --spider ${PBAPROTOCOL}://${PBAREPO}/ >/dev/null 2>/dev/null || wget --spider ${PBAPROTOCOL}://${PBAREPO}/ || exit 21
      
      # collect and conut Packages and Architectures, only if number is equal can we continue
      PBAARCHS=$(egrep "^Architecture: " ${DEBCONTROL} |cut -f2 -d' ' |sed -e 's/any/amd64/g')
      PBAPKGS=$(egrep "^Package: " ${DEBCONTROL} |cut -f2 -d' ')
      
      NR_PBAARCHS=$(echo "${PBAARCHS}" |wc -w |tr -d ' ')
      NR_PBAPKGS=$(echo "${PBAPKGS}" |wc -w |tr -d ' ')
      if [ "${NR_PBAARCHS}" != "${NR_PBAPKGS}" ] ; then
        echo "FATAL: In ${DEBCONTROL} found different numbers of ${NR_PBAARCHS} '^Architecture: ' and ${NR_PBAPKGS} '^Package: ' lines, aborting"
        exit 22
      fi
      for I in $(seq 1 ${NR_PBAARCHS}) ; do
        PBAARCH=$(egrep "^Architecture: " ${DEBCONTROL} |cut -f2 -d' ' |head -${I} |tail -1 |sed -e 's/any/amd64/g')
        PBAPKG=$(egrep "^Package: " ${DEBCONTROL} |cut -f2 -d' ' |head -${I} |tail -1)
        WAIT4DEB_URL="${PBAPROTOCOL}://${PBAREPO}/${PBADISTFAMILY}/pool/${PBADISTRO}-${PBACAT}-${PBAGEN}/${PBAPKG}_${DEBVER}_${PBAARCH}.deb"
        echo "INFO: For package ${PBAPKG}, ${I} of ${NR_PBAARCHS} in ${DEBCONTROL}, computed URL to ${WAIT4DEB_URL}"
        # https?://deb.foobar.com/any/pool/any-cat-1.x/foobar_1.y_all.deb
        if [ -z "${WAIT4DEB_URLS}" ] ; then
          WAIT4DEB_URLS="${WAIT4DEB_URL}"
        else
          WAIT4DEB_URLS="${WAIT4DEB_URLS} ${WAIT4DEB_URL}"
        fi
      done
      echo "GOOD: Done processing ${DEBCONTROL} repo ${PBAREPO} distro ${PBAARCH} ..."
    done
    echo "GOOD: Done processing ${DEBCONTROL} repo ${PBAREPO} ..."
  done
  
  echo "GOOD: Done parsing ${DEBCONTROL}"
done

echo "GOOD: Computed WAIT4DEB_URLS to '${WAIT4DEB_URLS}', appending this to ${UPSTREAMENV}"

echo 'WAIT4DEB_URLS="'"${WAIT4DEB_URLS}"'"' >>${UPSTREAMENV}
echo 'WAIT4DEB_PBAGENS="'"${WAIT4DEB_PBAGENS}"'"' >>${UPSTREAMENV}
