#!/bin/bash

set -e

# Get the number of seconds until the specified suite distribution expires
# at the repository supplied at the command line.

# The first argument should have the form something like this:
#   http://debian.stanford.edu/debian
#
# The second argument is a suite name and MUST be one of these:
#
#  - stable-proposed-updates
#  - stable-updates
#  - stable-backports
#  - testing
#  - testing-backports
#  - testing-proposed-updates
#  - testing-updates
#  - sid
#
# The third argument is OPTIONAL and should only be included if checking
# the security archive. If included it must equal to the string "security" AND
# the second argument must be one of "stable", or "oldstable".

REPOSITORY=$1
SUITENAME=$2
SECURITY=$3

USAGE=$(cat <<'EOF'
USAGE:   check-repo-inrelease-valid <debian-repo-dev> <suite-name> ["security"]

Examples:
  check-repo-inrelease-valid http://debian.stanford.edu/debian testing
  check-repo-inrelease-valid http://debian.stanford.edu/debian-security stable security
EOF
)

# Make sure the first two arguments are present:
if [[ -z "$REPOSITORY" || -z "$SUITENAME" ]]; then
  echo "$USAGE"
  exit 0
fi

valid_suites="sid"
valid_suites="${valid_suites}|oldstable-proposed-updates"
valid_suites="${valid_suites}|oldstable-updates"
valid_suites="${valid_suites}|stable-proposed-updates"
valid_suites="${valid_suites}|stable-updates"
#valid_suites="${valid_suites}|stable-backports"
valid_suites="${valid_suites}|testing"
valid_suites="${valid_suites}|testing-backports"
valid_suites="${valid_suites}|testing-proposed-updates"
valid_suites="${valid_suites}|testing-updates"

# Is the "security" option set?
if [[ "$SECURITY" == "security" ]]; then
  if [[ ! ("$SUITENAME" =~ ^(testing|stable|oldstable)$) ]]; then
    echo "when using the 'security' argument you can only use testing, stable, or oldstable"
    exit 1
  fi

  # The path for security updates changed for the bullseye release. The path for
  # security updates for bullseye and beyond is now <release>-security. Since bookworm
  # is not in official release we can use this path for all the security suites we mirror.
  URLPATH="${REPOSITORY}/dists/${SUITENAME}-security/InRelease"
else
  # Make sure the second argument is a valid suite name
  if [[ ! ("$SUITENAME" =~ ^(${valid_suites})$) ]]; then
    echo "invalid suite name '$SUITENAME'; must be one of stable|testing|sid"
    exit 1
  fi

  URLPATH="${REPOSITORY}/dists/${SUITENAME}/InRelease"
fi

export PATH="/usr/sbin:/usr/bin:/bin"

VALID_UNTIL_DATE=$(curl -s "${URLPATH}" 2>&1  | grep Valid-Until | cut -c 19-30)

#So, since your dates are in the past:
CURRENT_EPOCH=$(date +%s)
VALID_UNTIL_EPOCH=$(date --date="$VALID_UNTIL_DATE" +%s)

DIFF_SECS=$((VALID_UNTIL_EPOCH - CURRENT_EPOCH))

#echo "The difference is: $DIFF_SECS"
echo "$DIFF_SECS"

exit $?

DOCS=<<_END_OF_DOCS_

=head1 NAME

check-repo-inrelease-valid - Number of seconds until the suite expires

=head1 SYNOPSIS

B<check-repo-inrelease-valid>

=head1 DESCRIPTION

Returns the number of seconds left before distro got expired

=head1 AUTHOR

Srinivas Rao Puttagunta <psr123@stanford.edu>

=cut

_END_OF_DOCS_
