#!/bin/sh

set -e

# create a debian repo
# see: https://earthly.dev/blog/creating-and-hosting-your-own-deb-packages-and-apt-repo/
#
# put the .deb files e.g. in 
#   /var/www/html/tmp/deb/dists/stable/main/binary-amd64
#   /var/www/html/tmp/deb/dists/stable/non-free/binary-amd64
# execute this script to install the Debian repo.
# Once done, run any of
#   deb-repository.install -u
#   /usr/local/bin/update-deb-$VERSION
# every time there is a new .deb in the pool.
#
# Usage: deb-repository.install
#   -u  update the Debian packages
#   -v  VERS=oldstable|stable|testing|unstable

# ------------------------------------------------------------------------------

# the WWW path on the HOST server
WWW=tmp/debian

# the root of the Debian repo
DEB=/var/www/html/$WWW

# The default version of the Debian repo to handle
VERSION=stable

# ------------------------------------------------------------------------------

UPDATE_DEB=0

while getopts huv: flag
do
    case "${flag}" in
    h) # help
    "Usage: $0 [-huc]" 1>&2; 
    exit
    ;;
    u) # update
    UPDATE_DEB=1
    ;;
    v) # version
    VERSION=${OPTARG}
    ;;
    esac
done

# ------------------------------------------------------------------------------

# REPO contains the repository metadata (deb, Packages)
#   .deb in $REPO/main/binary-amd64
#   .deb in $REPO/non-free/binary-amd64
REPO=${DEB}/dists/$VERSION

export DEBIAN_FRONTEND=noninteractive
HOST=`hostname --fqdn`

echo "Configure a local Debian repository on $HOST"
apt install --ignore-missing -y -qq apache2

echo "Debian repository in https://$HOST/$WWW/dists/$VERSION"
echo "Debian packages   in $REPO/main/binary-amd64"
echo "Debian packages   in $REPO/non-free/binary-amd64"

mkdir -p $REPO/main/binary-amd64
mkdir -p $REPO/non-free/binary-amd64

FILE="/usr/local/bin/update-deb-$VERSION"
if [ -f "$FILE" ]; then
  echo "Using $FILE for the deb update"
else
  UPDATE_DEB=1
fi

if [ "x$UPDATE_DEB" = "x1" ]; then
  echo "Create $FILE to scan $REPO"
  dd status=none of=${FILE} << EOF
#!/bin/sh
# see: https://linuxopsys.com/topics/create-your-own-repository-for-packages-on-debian

echo "scanning .deb in $REPO/main to create https://$HOST/$WWW repository"
echo "dpkg-scanpackages --arch amd64 ${REPO}/main/binary-amd64 > ${REPO}/main/binary-amd64/Packages"

dpkg-scanpackages --arch amd64 ${REPO}/main/binary-amd64 > ${REPO}/main/binary-amd64/Packages
cat ${REPO}/main/binary-amd64/Packages | gzip -9 > ${REPO}/main/binary-amd64/Packages.gz


echo "scanning .deb in $REPO/non-free to create https://$HOST/$WWW repository"
echo "dpkg-scanpackages --arch amd64 ${REPO}/non-free/binary-amd64 > ${REPO}/non-free/binary-amd64/Packages"

dpkg-scanpackages --arch amd64 ${REPO}/non-free/binary-amd64 > ${REPO}/non-free/binary-amd64/Packages
cat ${REPO}/non-free/binary-amd64/Packages | gzip -9 > ${REPO}/non-free/binary-amd64/Packages.gz

# print information
echo "Run $FILE every time you add new .deb into $REPO"
echo "Add the following line to /etc/apt/sources.list.d/soleil-grades.list"
echo "  deb [arch=amd64 trusted=yes allow-insecure=yes] https://$HOST/$WWW $VERSION main non-free"

echo "==== SOLEIL GRADES Debian $VERSION Repository ====" > $REPO/README
echo "It contains internal Debian binary packages" >> $REPO/README
echo "Add the following line to your /etc/apt/sources.list.d/soleil-grades.list" >> $REPO/README
echo "  deb [arch=amd64 trusted=yes allow-insecure=yes] https://$HOST/$WWW $VERSION main non-free" >> $REPO/README
EOF
  chmod a+x $FILE

  # execute it
  ($FILE)
fi # UPDATE_DEB

echo "When adding deb packages in directory: $REPO"
echo "  $FILE"
echo "Add the following line to /etc/apt/sources.list.d/soleil-grades.list"
echo "  deb [arch=amd64 trusted=yes allow-insecure=yes] https://$HOST/$WWW $VERSION main non-free"

