#!/bin/sh

set -e -x -u

FORCE=${FORCE:=0}
TIER=${TIER:=nightly}
VERSION=$(cat /etc/debian_version)
VER=${VERSION%%.*}

case $VERSION in
4.*) CODENAME=etch ;;
5.*) CODENAME=lenny ;;
6.*) CODENAME=squeeze ;;
7.*) CODENAME=wheezy ;;
8.*) CODENAME=jessie ;;
9.*) CODENAME=stretch ;;
10.*) CODENAME=buster ;;
11.*) CODENAME=bullseye ;;
12.*) CODENAME=bookworm ;;
*) echo "boostrap-aptly: unknown or unsupported OS version '$VERSION'"; exit 2;;
esac

repository_debian_official_release() {
  # Usage: repository_debian_official_release
  if [ $CODENAME = "bookworm" ]; then
    cat > /etc/apt/sources.list.d/base_release_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-release/ $CODENAME main contrib non-free non-free-firmware
.eof
  else
    cat > /etc/apt/sources.list.d/base_release_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-release/ $CODENAME main contrib non-free
.eof
  fi
}

repository_debian_official_security() {
  # Usage: repository_debian_official_security
  if [ $CODENAME = "bookworm" ]; then
    cat > /etc/apt/sources.list.d/base_security_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-security/ $CODENAME-security main contrib non-free non-free-firmware
.eof
  elif [ $CODENAME = "bullseye" ]; then
    cat > /etc/apt/sources.list.d/base_security_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-security/ $CODENAME-security main contrib non-free
.eof
  else
    cat > /etc/apt/sources.list.d/base_security_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-security/ $CODENAME-updates main contrib non-free
.eof
  fi
}

repository_debian_official_updates() {
  # Usage: repository_debian_official_updates
  if [ $CODENAME = "bookworm" ]; then
    cat > /etc/apt/sources.list.d/base_updates_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-updates/ $CODENAME-updates main contrib non-free non-free-firmware
.eof
  elif [ $CODENAME = "bullseye" ]; then
    cat > /etc/apt/sources.list.d/base_updates_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-updates/ $CODENAME-updates main contrib non-free
.eof
  else
    cat > /etc/apt/sources.list.d/base_updates_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-official-updates/ $CODENAME-updates main contrib non-free
.eof
  fi
}

repository_lfnet_apt() {
  # Usage: repository_lfnet_apt
  cat > /etc/apt/sources.list.d/base_apt_via_puppet.list <<.eof
deb http://aptly.lf.net/$TIER/debian-$VER-lfnet-apt/ $CODENAME main non-free
.eof
}

if [ "$CODENAME" = "etch" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl http://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_lfnet_apt
  apt-get update
  echo "Unsupported system, please upgrade!"
fi

if [ "$CODENAME" = "lenny" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl http://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_lfnet_apt
  apt-get update
  echo "Unsupported system, please upgrade!"
fi

if [ "$CODENAME" = "squeeze" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl http://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_lfnet_apt
  apt-get update
  echo "Unsupported system, please upgrade!"
fi

if [ "$CODENAME" = "wheezy" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl https://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_lfnet_apt
  apt-get update
  echo "Unsupported system, please upgrade!"
fi

if [ "$CODENAME" = "jessie" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl https://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_lfnet_apt
  apt-get update
  echo "Unsupported system, please upgrade!"
fi

if [ "$CODENAME" = "stretch" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl https://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_debian_official_updates
  repository_lfnet_apt
  apt-get update
  echo "Unsupported system, please upgrade!"
fi

if [ "$CODENAME" = "buster" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl https://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_debian_official_updates
  repository_lfnet_apt
  apt-get update
fi

if [ "$CODENAME" = "bullseye" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl https://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  repository_debian_official_release
  repository_debian_official_security
  repository_debian_official_updates
  repository_lfnet_apt
  apt-get update
fi

if [ "$CODENAME" = "bookworm" ]
then
  export DEBIAN_FRONTEND=noninteractive
  curl https://aptly.lf.net/linux_signing_key.pub | sudo apt-key add -
  if [ "$FORCE" = "1" ]; then
    echo "FORCE"
    rm -rf /etc/apt.sources.list.d
  fi
  mkdir -p /etc/apt/sources.list.d
  # TODO: non-free-firmware
  repository_debian_official_release
  repository_debian_official_security
  repository_debian_official_updates
  repository_lfnet_apt
  apt-get update
fi

exit 0
