#!/bin/bash

set -e

SCRIPT_PATH=$(dirname $(realpath $0))
TARGET=${SCRIPT_PATH}/sign-deb-package.sh
EOF_MARKER="# Script EOF"

if [ $# -gt 0 ]; then
	TARGET=$1
fi

grep "^${EOF_MARKER}" ${TARGET} >/dev/null 2>&1 || { echo "Error: no EOF marker in ${TARGET}"; exit 1; }
echo "Updating packages in ${TARGET}"

TARGET=$(realpath ${TARGET})

TMP_TARGET=$(mktemp $(basename $0).XXXXXX)
trap "rm -f ${TMP_TARGET}" INT TERM EXIT

sed "/^${EOF_MARKER}/q" < ${TARGET} > ${TMP_TARGET}

pushd ${SCRIPT_PATH} >/dev/null 2>&1
tar cvz keyring-1.4/*_all.deb keyring-1.5/*_all.deb >> ${TMP_TARGET}
popd >/dev/null 2>&1

cp ${TMP_TARGET} ${TARGET}

exit 0

