#!/bin/sh

set -e

  # Verify command line arguments
ymd="$1"
test -n "$1" || {
  echo "must provide number of snapshot to release" 1>&2
  exit 1
}

  # Verify environment correctness
. tools/include/config || {
  echo "must be run from mod-xslt source top level directory" 1>&2
  exit 1
}

  # Check existance of site mirror
test -d "$dir_mirror" || {
  echo "couldn't find mod-xslt site mirror directory."
  echo "Without that, this script is almost useless"
  exit 1
}

  # Verify which version this release would be
curr_patchlevel=`$cmd_tla revisions -A $tla_archive $tla_brench_stable |tail -n1|cut -d'-' -f2`
curr_patchlevel=`expr $curr_patchlevel + 1`
curr_version="$tla_brench_stable_version"
echo "Releasing: $curr_version.$curr_patchlevel ($tla_archive/$tla_brench_stable)"

  # Ok, calculate current versions
. $dir_library/get-versions
  
  # Verify configure is up to date
test "$lib_version_configac" = "${curr_version}.${curr_patchlevel}" || {
  echo "  - need to update $prj_configure ($lib_version_configac -> ${curr_version}.${curr_patchlevel})"
  echo "please release a new snapshot first"
  exit 1
}

  # Verify configure in archive
prj_configac="${dir_repository}/$ymd/$prj_name-$ymd/configure.ac"
prj_configure="${dir_repository}/$ymd/$prj_name-$ymd/configure"
. $dir_library/get-versions

  # Verify configure in the specified snapshot are up to date
test "$lib_version_configac" = "${curr_version}.${curr_patchlevel}" || {
  echo "  - specified snapshot has version $lib_version_configac while project ${curr_version}.${curr_patchlevel}"
  echo "please release a new snapshot with correct version first"
  exit 1
}

  # Verify configure.ac and configure have the same version
test "$lib_version_configac" = "$lib_version_configure" || {
  echo "  - configure.ac and configure have out of sync version numbers in specified snapshot"
  echo "please release a new snapshot with correct versions first"
  exit 1
}

# uhm, last snapshot and current tree are not directly
# related. However, if the snapshot had changes, it probably
# does make sense to avoid it being release. I can always
# brench has much as I want with TLA

  # Now, verify we can tag this version has the latest version
$cmd_tla changes &>/dev/null || {
  echo "  - current tree is different than last commit. Not releasing."
  echo "please release a new snapshot first"
  test -n "$SCRIPT_DEBUG" || exit 1
}

  # Also make sure all patches are in latest snapshot 
test "${tla_brench_current}--`$cmd_tla revisions |tail -n1`" = "$prj_last_snapshot" || {
  echo "  - last snapshot doesn't have all committed patches. Not releasing (${tla_brench_current}, $prj_last_snapshot)"
  echo "please release a new snapshot first"
  exit 1
}

  # now, I have 2 things to do:
  #  - tag the last snapshot as current release
#$cmd_tla tag -A "$tla_archive" "$prj_last_snapshot" "${tla_brench_stable}--patch-${curr_patchlevel}"
$cmd_tla tag -A "$tla_archive" "$prj_last_snapshot" "${tla_brench_stable}"

  #  - update the release file
library_mmp="${curr_version}.${curr_patchlevel}"
library_ymd="$ymd"
library_file="$file_releases"

. $dir_library/changelog-update
. $dir_library/changelog-checksums
