#!/bin/bash

if [ -z ${JENKINS_DEBIAN_GLUE_QUIET:-} ]; then
  set -x
fi
set -e

bailout() {
  [ -n "${1:-}" ] && EXIT="${1}" || EXIT=0
  [ -n "${2:-}" ] && echo "$2" >&2
  rm -f /var/lock/jdg-generate-reprepro-codename."$(id -un)"
  exit $EXIT
}

# backwards compatibility, see PR#94
if [ -z "${REPOSITORY:-}" ] ; then
  repository_is_missing_in_env=true
else
  repository_is_missing_in_env=false
fi

if [ -r /etc/jenkins/debian_glue ] ; then
  echo "*** Sourcing /etc/jenkins/debian_glue ***"
  . /etc/jenkins/debian_glue
fi

# backwards compatibility, see PR#94
if [ -n "${REPOSITORY:-}" ] && $repository_is_missing_in_env ; then
  echo "*** WARNING: 'REPOSITORY' set in /etc/jenkins/debian_glue but should be DEFAULT_REPOSITORY ***"
  echo "*** WARNING: Setting DEFAULT_REPOSITORY to $REPOSITORY for backwards compatibility ***"
  echo "*** WARNING: Please replace REPOSITORY=... in /etc/jenkins/debian_glue with DEFAULT_REPOSITORY=... ***"
  DEFAULT_REPOSITORY="${REPOSITORY}"
fi

if [ "${BUILD_ONLY:-}" = "true" ] ; then
  echo "BUILD_ONLY is set to true, ignoring request to generate local repository."
  exit 0
fi

ARCHITECTURES="${ARCHITECTURES:-amd64 i386 source}"
COMPONENTS="${COMPONENTS:-main}"
UDEB_COMPONENTS="${UDEB_COMPONENTS:-main}"

usage() {
  echo "Usage: $0" \
       "[ --architectures <architectures> ]" \
       "[ --components <components> ]" \
       "[ --udeb-components <udeb components> ]" \
       "[ --origin <origin> ]" \
       "[ --suite <suite> ]" \
       "[ --label <label> ]" \
       "[ --version <version> ]" \
       "<codename>"
}


while [ "$#" -gt 1 ]; do
    case "$1" in
      --architectures)
        ARCHITECTURES="$2"
        shift 2
      ;;
      --components)
        COMPONENTS="$2"
        shift 2
      ;;
      --udeb-components)
        UDEB_COMPONENTS="$2"
        shift 2
      ;;
      --origin)
        ORIGIN="$2"
        shift 2
      ;;
      --suite)
        SUITE="$2"
        shift 2
      ;;
      --label)
        LABEL="$2"
        shift 2
      ;;
      --version)
        VERSION="$2"
        shift 2
      ;;
      --)
        shift; break
      ;;
      *)
        echo "Usage error." >&2
        usage >&2
        exit 1
      ;;
    esac
done

if [ "$#" -lt 1 ] ; then
  usage >&2
  exit 1
fi

# repository/codename that should be added
REPOS="$1"

JENKINS_DEBIAN_GLUE_VERSION=$(dpkg --list jenkins-debian-glue 2>/dev/null | awk '/^ii/ {print $3}')
if [ -n "${JENKINS_DEBIAN_GLUE_VERSION:-}" ] ; then
  echo "*** Running jenkins-debian-glue version $JENKINS_DEBIAN_GLUE_VERSION ***"
fi

if [ -z "${DEFAULT_REPOSITORY:-}" ] ; then
  echo "*** Repository variable DEFAULT_REPOSITORY is unset, using default [$DEFAULT_REPOSITORY] ***"
  DEFAULT_REPOSITORY='/srv/repository'
fi

# REPOSITORY can overwrite DEFAULT_REPOSITORY, so define only if unset
if [ -z "${REPOSITORY:-}" ] ; then
  REPOSITORY="${DEFAULT_REPOSITORY}"
  echo "*** Repository variable REPOSITORY is unset, using default [$REPOSITORY] ***"
fi

if ! mkdir -p "${REPOSITORY}"/conf ; then
  echo "Error creating ${REPOSITORY}/conf (forgot to create ${REPOSITORY} and chown jenkins?)" >&2
  exit 1
fi

if ! chown $(id -un) "${REPOSITORY}"/conf ; then
  echo "*** Warning: failed to adjust permissions of ${REPOSITORY}/conf ***"
  echo "*** This might be caused by a remote FS like sshfs, so not failing the build. ***"
  echo "*** Please fix the underlying problem if you depend on according permissions. ***"
fi
touch "${REPOSITORY}"/conf/distributions

# support setting key id
if [ -z "${KEY_ID:-}" ] ; then
  echo "*** WARNING: No KEY_ID found, can not sign repository. ***"
  echo "***          Generate a key executing 'gpg --gen-key' as user root"
  echo "***          and then adjust /etc/jenkins/debian_glue."
fi

# lock access to file to avoid duplicate entries when two jdg-generate-reprepro-codename
# runs happen at the very same time
(
flock --timeout 5 9 || bailout 1 "Error: could not lock file ${REPOSITORY}/conf/distributions, giving up."

if grep -q "^\(Codename\|Suite\): ${REPOS}$" "${REPOSITORY}"/conf/distributions ; then
  echo "Codename/repository $REPOS exists already, ignoring request to add again."
  exit 0
fi

cat >> "${REPOSITORY}"/conf/distributions << EOF

Codename: ${REPOS}
Architectures: ${ARCHITECTURES}
Components: ${COMPONENTS}
EOF

if [ -n "${SUITE:-}" ]; then
  printf "Suite: ${SUITE}\n" >> "${REPOSITORY}"/conf/distributions
fi

if [ -n "${LABEL:-}" ]; then
  printf "Label: ${LABEL}\n" >> "${REPOSITORY}"/conf/distributions
fi

if [ -n "${VERSION:-}" ]; then
  printf "Version: ${VERSION}\n" >> "${REPOSITORY}"/conf/distributions
fi

if [ -n "${ORIGIN:-}" ]; then
  printf "Origin: ${ORIGIN}\n" >> "${REPOSITORY}"/conf/distributions
fi

if [ -n "${UDEB_COMPONENTS:-}" ]; then
  printf "UDebComponents: ${UDEB_COMPONENTS}\n" >> "${REPOSITORY}"/conf/distributions
fi

printf "Tracking: minimal\n" >> "${REPOSITORY}"/conf/distributions

if [ -n "${KEY_ID:-}" ] ; then
  echo "*** Signing repository with Key ID $KEY_ID ***"
  printf "SignWith: ${KEY_ID}\n" >> "${REPOSITORY}"/conf/distributions
fi

echo "Added $REPOS as new codename/repos to the reprepro configuration."

) 9>/var/lock/jdg-generate-reprepro-codename."$(id -un)" || bailout 1 "Error while generating reprepro codename/repos."

rm -f /var/lock/jdg-generate-reprepro-codename."$(id -un)"

# vim:foldmethod=marker ts=2 ft=sh ai expandtab sw=2
