#!/usr/bin/bash
# To be used by client with the command:
#   sh -x <(curl https://ftp.eso.org/pub/vlt/repos/GetVLTSW.sh) [-s paranal|garching|obshq]
# or
#   sh -x <(curl https://ftp.eso.org/pub/vlt/repos/GetVLTSW.sh) <release> [-s paranal|garching]

set -e

# Determine default vltsw version
vltswver=$(
  source /etc/os-release
  if [[ ${ID} == almalinux ]] && [[ ${VERSION_ID} == 10.* ]]; then
    echo 2026.latest
  fi
)

# Default yum repo site (Paranal/Garching)
reposite="garching"

usage() {
  cat <<EOF
To be used by client with the command:
   sh -x <(curl https://ftp.eso.org/pub/vlt/repos/$(basename "$0"))

Usage: $(basename "$0") [OPTIONS] <release>
Default release: ${vltswver}

OPTIONS:
  -s SITE     Repository site (garching, paranal, obshq) (default: ${reposite})
  -h, --help  Show this help message

EXAMPLES:
  $(basename "$0") 
  $(basename "$0") -s par 2026.latest
  $(basename "$0") -s obshq 


EOF
}

while [ $# -gt 0 ]; do
  case "$1" in
    -h|--help)
      usage
      exit 0
      ;;
    -s)
      reposite=$(printf '%s' "$2" | tr '[:upper:]' '[:lower:]')
      shift 2
      ;;
    *)
      vltswver="$1"
      shift
      ;;
    esac
done

if [[ -z ${vltswver} ]]; then
  echo "VLTSW version is not defined"
  exit 1
fi


RPMNAME="vlt-vltsw-ansible"
SCRIPTNAME="ansible-force-align"

if rpm -q ${RPMNAME} >/dev/null 2>&1 ; then
 echo "RPM ${RPMNAME} already installed"
 exit 0
fi

# ELTDEV-1156
MINSIZE=20
size_root=`/usr/bin/df -H / | tail -1 | gawk '{print $2}' | sed 's/G//'`
if [ $size_root -le $MINSIZE ]; then
  echo ""
  echo "WARNING: The root partition might not big enough (>${MINSIZE}G):"
  df -H /
  freesize=`vgs | tail -1 | gawk '{print $7}'`
  if [ "$freesize" !=  "0" ]; then
    vgs
    echo ""
    echo "Some free disk space still available for the root partition"
    echo "You may extend it, as root, with the command:"
    echo "  lvextend /dev/mapper/fedora_fedora-root -l100%FREE"
    echo "  xfs_growfs /dev/mapper/fedora_fedora-root"
  fi
  echo ""
  echo -n "Do you want to continue [yn] (n)? : "
  read answ
  if [ -z "$answ" -o "$answ" != "y" ]; then
    exit 0
  fi
fi

# ESO VLTSW repo
if [[ ${vltswver} == 2026.* ]]; then
  vltswrepo=$(
    case ${reposite} in
      par|paranal)
        echo "https:///yum.pl.eso.org/pub/yum/vltsw/2026/${vltswver}"
        ;;
      obs|obshq)
        echo "http://obs.hq.eso.org:82/vlt:/vltsw:/2026/vlt_almalinux_10/"
        ;;
      *)
        echo "https://ftp.eso.org/pub/vlt/repos/eso/vltsw/2026/${vltswver}"
        ;;
    esac
  )
else
  echo "ERROR: Invalid VLTSW version ${vltswver}"
  exit 1
fi

# Check the vltsw repository exists and usable by DNF:
if ! curl -fs "${vltswrepo}/repodata/repomd.xml" >/dev/null; then
    echo "ERROR: Invalid repository: ${vltswrepo}"
    exit 1
fi

echo ${vltswrepo}
vltswrepo_rpm="vlt-vltsw-repos"
if [[ ${reposite} == par* ]]; then
   vltswrepo_rpm="vlt-vltsw-paranal-repos"
fi
echo ${vltswrepo_rpm}

dnf clean all
dnf -y --nogpgcheck --disablerepo="*" --repofrompath vltsw,${vltswrepo} install ${vltswrepo_rpm}

if [ $? -ne 0 ]; then
  echo "Download of RPM vlt-vltsw-repos failed"
  exit 1
fi


echo $vltswver > /etc/dnf/vars/vltswver

if [[ ${reposite} == obs* ]]; then
  dnf config-manager --set-enabled vlt-vltsw-obs
fi

dnf -y install ${RPMNAME}

cd /root/vlt
echo "Executing: ${SCRIPTNAME}"
./${SCRIPTNAME}

exit $?
