#!/bin/bash
set -e

SELFROOT=`dirname "$0"`
SELFROOT=`cd "$SELFROOT" && pwd`
TEMPDIR=

PACKAGE_DIR=
SIGNING_KEY=
PASSPHRASE_FILE=

function usage()
{
  echo "Usage: ./repo_update [OPTIONS]"
  echo "Sign RPMs and import them into a YUM repository."
  echo
  echo "Required options:"
  echo "  -p PATH        Path to a project"
  echo
  echo "  -r PATH        Directory to RPMs"
  echo "  -P PATH        Directory to YUM repository"
  echo
  echo "  -s KEY         Signing key to use"
  echo "  -x PATH        File containing signing key passphrase"
}

function parse_options()
{
  local OPTIND=1
  local opt
  while getopts "p:r:P:s:x:h" opt; do
    case "$opt" in
    p)
      PACKAGE_DIR="$OPTARG/build"
      REPO_DIR="$OPTARG/repo"
      ;;
    r)
      PACKAGE_DIR="$OPTARG"
      ;;
    P)
      REPO_DIR="$OPTARG"
      ;;
    s)
      SIGNING_KEY="$OPTARG"
      ;;
    x)
      PASSPHRASE_FILE="$OPTARG"
      ;;
    h)
      usage
      exit
      ;;
    *)
      return 1
      ;;
    esac
  done

  if [[ "$PACKAGE_DIR" = "" ]]; then
    echo "Please specify a project using -p, or an RPM package directory using -r."
    exit 1
  fi
  if [[ "$REPO_DIR" = "" ]]; then
    echo "Please specify a project using -p, or a YUM repo directory using -P."
    exit 1
  fi
  if [[ "$SIGNING_KEY" = "" ]]; then
    echo "Please specify a signing key using -s."
    exit 1
  fi
  if [[ "$PASSPHRASE_FILE" = "" ]]; then
    echo "Please specify a passphrase file using -p."
    exit 1
  fi
}

function cleanup()
{
  local pids=`jobs -p`
  set +e
  if [[ "$pids" != "" ]]; then
    kill $pids
  fi
  if [[ "$TEMPDIR" != "" ]]; then
    rm -rf "$TEMPDIR"
  fi
}

trap cleanup EXIT
parse_options "$@"

if ! gpg --list-keys "$SIGNING_KEY" >/dev/null 2>/dev/null; then
  echo "ERROR: signing key $SIGNING_KEY not found."
  exit 1
fi

APP_UID=`id -u`
APP_GID=`id -g`
TEMPDIR=`mktemp -d /tmp/passenger-rpm.XXXXXXXX`
gpg --armor --export-secret-keys "$SIGNING_KEY" > "$TEMPDIR/signing_key"
cat "$PASSPHRASE_FILE" > "$TEMPDIR/signing_passphrase"
chmod 600 "$TEMPDIR"/*

docker run --rm \
  -v "$SELFROOT:/system:ro" \
  -v "$TEMPDIR:/params:ro" \
  -v "$PACKAGE_DIR:/packages" \
  -v "$REPO_DIR:/repo" \
  -e "APP_UID=$APP_UID" \
  -e "APP_GID=$APP_GID" \
  -e "SIGNING_KEY=$SIGNING_KEY" \
  phusion/passenger_rpm_automation \
  /system/internal/my_init --skip-runit --skip-startup-files --quiet -- \
  /system/internal/repo_update
