#!/bin/bash

set -e
set -u

export APPSH_HOME=$(cd $(dirname "$0")/.. && pwd)

. $APPSH_HOME/lib/common
# HEADER END

usage_text() {
  echo "usage: $usage_app -v [version] -h [hook]"
}

version=
hook=

while getopts "v:h:" opt
do
  case $opt in
    v)
      version=$OPTARG
      shift 2
      OPTIND=1
      ;;
    h)
      hook=$OPTARG
      shift 2
      OPTIND=1
      ;;
  esac
done

if [[ -z $version || -z $hook || $# != 0 ]]
then
  usage
fi

bin=versions/$version/hooks/$hook

if [[ ! -r $bin ]]
then
  exit 0
fi

chmod +x $bin

if [[ ! -e $bin ]]
then
  info "Not executable: $bin"
  exit 0
fi

debug "Running hook: $hook"
run_app -v $version hooks/$hook