#!/bin/sh
# SPDX-License-Identifier: MIT

# "Generic" Fedora.
COMPOSE_F34=https://download.fedoraproject.org/pub/fedora/linux/releases/34/Everything
COMPOSE_F35=https://download.fedoraproject.org/pub/fedora/linux/releases/35/Everything
COMPOSE_F36=https://download.fedoraproject.org/pub/fedora/linux/releases/36/Everything
COMPOSE_F37=https://download.fedoraproject.org/pub/fedora/linux/releases/37/Everything
COMPOSE_F38=https://download.fedoraproject.org/pub/fedora/linux/releases/38/Everything
COMPOSE_F39=https://download.fedoraproject.org/pub/fedora/linux/releases/39/Everything
COMPOSE_F40=https://download.fedoraproject.org/pub/fedora/linux/releases/40/Everything
COMPOSE_F41=https://download.fedoraproject.org/pub/fedora/linux/releases/41/Everything
COMPOSE_F42=https://download.fedoraproject.org/pub/fedora/linux/releases/42/Everything
COMPOSE_F43=https://download.fedoraproject.org/pub/fedora/linux/releases/43/Everything
COMPOSE_F44=https://download.fedoraproject.org/pub/fedora/linux/releases/44/Everything
COMPOSE_RAWHIDE=https://download.fedoraproject.org/pub/fedora/linux/development/rawhide/Everything

[ -z "${RELEASE}" ] && RELEASE=rawhide

case "${RELEASE}" in
34|f34|fc34)
  log "Using Fedora 34 template"
  HOST=${HOST:-f34}
  COMPOSE="${COMPOSE:-${COMPOSE_F34}}"
  ;;
35|f35|fc35)
  log "Using Fedora 35 template"
  HOST=${HOST:-f35}
  COMPOSE="${COMPOSE:-${COMPOSE_F35}}"
  ;;
36|f36|fc36)
  log "Using Fedora 36 template"
  HOST=${HOST:-f36}
  COMPOSE="${COMPOSE:-${COMPOSE_F36}}"
  ;;
37|f37|fc37)
  log "Using Fedora 37 template"
  HOST=${HOST:-f37}
  COMPOSE="${COMPOSE:-${COMPOSE_F37}}"
  ;;
38|f38|fc38)
  log "Using Fedora 38 template"
  HOST=${HOST:-f38}
  COMPOSE="${COMPOSE:-${COMPOSE_F38}}"
  ;;
39|f39|fc39)
  log "Using Fedora 39 template"
  HOST=${HOST:-f39}
  COMPOSE="${COMPOSE:-${COMPOSE_F39}}"
  ;;
40|f40|fc40)
  log "Using Fedora 40 template"
  HOST=${HOST:-f40}
  COMPOSE="${COMPOSE:-${COMPOSE_F40}}"
  ;;
41|f41|fc41)
  log "Using Fedora 41 template"
  HOST=${HOST:-f41}
  COMPOSE="${COMPOSE:-${COMPOSE_F41}}"
  ;;
42|f42|fc42)
  log "Using Fedora 42 template"
  HOST=${HOST:-f42}
  COMPOSE="${COMPOSE:-${COMPOSE_F42}}"
  ;;
43|f43|fc43)
  log "Using Fedora 43 template"
  HOST=${HOST:-f43}
  COMPOSE="${COMPOSE:-${COMPOSE_F43}}"
  ;;
44|f44|fc44)
  log "Using Fedora 44 template"
  HOST=${HOST:-f44}
  COMPOSE="${COMPOSE:-${COMPOSE_F44}}"
  ;;
rawhide)
  log "Using Fedora rawhide template"
  HOST=${HOST:-rawhide}
  COMPOSE="${COMPOSE:-${COMPOSE_RAWHIDE}}"
  ;;
*)
  die "Unsupported release (${RELEASE})"
  ;;
esac

resolve_compose() {
  _c="${1:-}"
  [ -z "${_c}" ] && return 1

  if ! _nc=$(curl -Lv "${_c}" 2>&1 \
             | grep -i ^'< location:' \
             | tr -d '\r' \
             | awk '{ print $3 }') || [ -z "${_nc}" ]; then
    return 1
  fi

  if curl -Lv "${_nc}" 2>&1 | grep ^'< HTTP/' | grep -q 200; then
    echo "${_nc}"
    return 0
  fi

  # The resolved URL does not work, for some reason. I have seen
  # some Portugal (PT) mirrors return a wrong URL like this:
  # > returned:  https://mirrors.ptisp.pt/fedora/releases/38/Everything/
  # > corrected: https://mirrors.ptisp.pt/fedora/linux/releases/38/Everything/
  # Let's try to fix this case.
  _fixed=
  case "${_nc}" in
  */fedora/releases/*)
    _fixed="$(echo "${_nc}" \
              | sed -e 's@/fedora/releases/@/fedora/linux/releases/@')"
    ;;
  esac

  [ -z "${_fixed}" ] && return 1


  if curl -Lv "${_fixed}" 2>&1 | grep ^'< HTTP/' | grep -q 200; then
    echo "${_fixed}"
    return 0
  fi
  return 1
}

if [ -n "${USE_ISO}" ]; then
  # ISO install — use the ISO path directly as LOCATION.
  LOCATION="${COMPOSE}"
else
  # Let's resolve the COMPOSE.
  if ! _compose="$(resolve_compose "${COMPOSE}")" \
                   || [ -z "${_compose}" ]; then
    echo ">> Unable to resolve Fedora compose ${COMPOSE} => ${_compose}" >&2
    exit 1
  fi

  # Adjusting compose - strip trailing slash to avoid double slashes in LOCATION.
  COMPOSE="$(echo "${_compose}" | sed 's@/$@@')"
  LOCATION="${COMPOSE}"/"${ARCH}"/os/
fi

IP=${IP:-${NET_RANGE}.222}
NAME=${NAME:-${HOST}-${TS}-r${SFX}}
FS_TYPE=btrfs
KS_TEMPLATE="${KS_DIR}"/ks.cfg.fedora.template

# vim:set ts=2 sw=2 et:
