#!/bin/sh

# first argument - path to cloned Psi repo
# second argument[optional] - --webkit

show_help() {
  echo "Usage: $0 <psi_repo_path> [--webkit|--webengine] [--sql]"
}

[ -d "${1}/.git" ] || { show_help; exit 1; }
psi_path="$(cd $1;pwd)"

cd "$(dirname "$0")"

# revs
psi_rev="$(cd "$psi_path"; git rev-parse --short HEAD)"
plus_rev="$(git rev-parse --short HEAD)"
plus_tag=$(git describe --tags | cut -d - -f1)

# compute version number
psi_num=$("${psi_path}/admin/git_revnumber.sh" "${plus_tag}")
plus_num=$(git describe --tags | cut -d - -f2)
[ "$(echo ${plus_num} | grep \\. | wc -l)" != 0 ] && plus_num=0
sum_commit=$(expr ${psi_num} + ${plus_num})

# compute version date
rev_date_list="$(cd "${psi_path}"; git log -n1 --date=short --pretty=format:'%ad')
$(git log -n1 --date=short --pretty=format:'%ad')"
rev_date=$(echo "${rev_date_list}" | sort -r | head -n1)

# features list
features=$([ "${2}" = --webkit ] && echo ", webkit")$([ "${2}" = --webengine ] && echo ", webengine")$([ "${2}" = --sql ] && echo ", sql")$([ "${3}" = --sql ] && echo ", sql")

echo "${plus_tag}.${sum_commit} (${rev_date}, Psi:${psi_rev}, Psi+:${plus_rev}${features})"
