#!/bin/sh
#
# Copyright (C) 2009  Robert Millan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

usage ()
{
  cat << EOF >&2
Usage: $0 --release RELEASE --arches ARCHES --description DESCRIPTION --gpg-key GPG-KEY --pkgdir PKGDIR [--override-file OVERRIDES]
	RELEASE is the target release name (e.g. "lenny")
	ARCHES is a space-separated list of architectures (e.g. "i386 amd64")
	DESCRIPTION is a human-readable description (e.g. "This is my archive")
	GPG-KEY is the Gnupg key ID that will be used to sign your archive (e.g. DEADBEEF)
	PKGDIR is the directory containing packages (e.g. "pool")
	OVERRIDES is an optionally-provided override file
EOF
}

if [ $# = 0 ] ; then
  usage
  exit 1
fi

backend=/usr/lib/vsag.mk

while [ $# -gt 0 ] ; do
  case "$1" in
    -h|--help)
      usage
      exit 0
    ;;
    --release)
      shift
      export release="$1"
    ;;
    --arches)
      shift
      export arches="$1"
    ;;
    --description)
      shift
      export description="$1"
    ;;
    --gpg-key)
      shift
      export key="$1"
    ;;
    --pkgdir)
      shift
      export pkgdir="$1"
    ;;
    --backend)
      shift
      backend="$1"
    ;;
    --override-file)
      shift
      export override_file="$1"
    ;;
    *)
      echo "$0: invalid option -- $1" >&2
      echo "Try \`$0 --help' for more information." >&2
      exit 1
    ;;
  esac
  shift
done

exec make -j`getconf _NPROCESSORS_ONLN || echo 1` -f ${backend} dists
