#!/usr/bin/bash

# Setup BEFORE running this script, since we will download the zip file generated by the git tag:
#  git tag vX.Y"
#  git commit -m 'message'"
#  git push"
#  git push origin vX.Y"

show_usage_and_quit() {
    echo "Usage: build_version <versiontag>"
    echo "where versiontag has the format #.# or #.#.#"
    exit
}

if [ "$#" -ne 1 ]; then
    show_usage_and_quit
fi

# Version number format is documented in show_usage_and_quit
VERSION=$1

echo "Cleaning old build directory"
rm -rf 'engauge-digitizer-'"$VERSION"'-1.x86_64' 2>/dev/null
echo

# Cygport file must have the format <package name>.cygport
CYGFILE=engauge-digitizer.cygport

echo "Creating engauge-digitizer.cygport file from engauge-digitizer.cygport.template"
dos2unix engauge-digitizer.cygport.template
sed 's/<VERSION>/'"$VERSION"'/' engauge-digitizer.cygport.template >$CYGFILE
echo

#DEBUG=--debug

cygport $DEBUG $CYGFILE download 2>&1 | tee build_version.log
cygport $DEBUG $CYGFILE prep     2>&1 | tee -a build_version.log
cygport $DEBUG $CYGFILE compile  2>&1 | tee -a build_version.log
cygport $DEBUG $CYGFILE install  2>&1 | tee -a build_version.log
cygport $DEBUG $CYGFILE package  2>&1 | tee -a build_version.log

echo "Output was logged to build_version.log"

echo "After testing, then upload according to https://cygwin.com/package-upload.html#uploading-files"

echo "After uploading, release according to https://cygwin.com/packaging-contributors-guide.html#acceptance"
