#!/bin/bash
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/eightprotons/ streams/v1/index.sjs
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
