#!/bin/sh

usage()
{
cat << EOF
usage: $0 options

OpenSC bootstrap
OPTIONS:
   -h      Show this message
   -s      Package suffix
   -S      Use package suffix as 'g' appended with the date of last commit
EOF
}


SUFFIX=
while getopts “:hs:S” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         s)
             SUFFIX=$OPTARG
             ;;
         S)
             SUFFIX=g`git log -1 --pretty=fuller --date=iso | grep CommitDate: | sed -E 's/^CommitDate:\s(.*)/\1/' | sed -E 's/(.*)-(.*)-(.*) (.*):(.*):(.*)\s+.*/\1\2\3\4\5\6/'`
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

set -e

if [ -f Makefile ]; then
  make distclean
fi

rm -rf *~ *.cache config.guess config.log config.status config.sub depcomp ltmain.sh version.m4.ci

if [ -n "$SUFFIX" ]
then
    echo Set package suffix "$SUFFIX"
    sed 's/^define(\[PACKAGE_SUFFIX\],\s*\[\([-~]*[0-9a-zA-Z]*\)\])$/define(\[PACKAGE_SUFFIX\], \['$SUFFIX'\])/g' < version.m4 > version.m4.ci
fi

./bootstrap
# autoreconf --verbose --install --force || true
