#!/bin/bash
#   Copyright (C) 2013 Canonical Ltd.
#
#   Author: Scott Moser <scott.moser@canonical.com>
#
#   Simplestreams is free software: you can redistribute it and/or modify it
#   under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or (at your
#   option) any later version.
#
#   Simplestreams 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 Affero General Public
#   License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with Simplestreams.  If not, see <http://www.gnu.org/licenses/>.
set -f
if [ "$1" = "--help" -o "$1" = "usage" ]; then
   cat <<EOF
Usage: ${0##*/}
  This program is an example 'hook' for use with bin/sstream-sync.
  PYTHONPATH=$PWD ./bin/sstream-sync --item-skip-download \\
     --hook=./tools/hook-debug \\
     http://cloud-images.ubuntu.com/releases/ streams/v1/index.sjson
EOF
fi

data=""
for f in ${FIELDS}; do
    val="${!f}"
    [ "${val#* }" != "${val}" ] && data="${data} ${f}='${val}'" ||
        data="${data} ${f}=${val}"
done
data=${data# }

[ -z "$HOOK" ] && { echo "HOOK not set. see usage --help"; exit 1; }

case "$HOOK" in
   filter_product|filter_version)
      # skip streams or groups that do not have arch=i386
      if [ "$arch" = "i386" ]; then
        OP="keep"
      else
        OP="skip"
      fi
      ;;
   filter_*) OP="keep";;
esac

echo "=== $HOOK: ${OP:+[${OP}]} ${data}" 1>&2
case "$HOOK" in
    filter_*)
        [ "$OP" = "keep" ] && exit 0;
        [ "$OP" = "skip" ] && exit 1;
        exit 2
        ;;
esac
exit 0
