#!/bin/sh

#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the Common Development
# and Distribution License Version 1.0 (the "License").
#
# You can obtain a copy of the license at
# http://www.opensource.org/licenses/CDDL-1.0.  See the License for the
# specific language governing permissions and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each file and
# include the License file in a prominent location with the name LICENSE.CDDL.
# If applicable, add the following below this CDDL HEADER, with the fields
# enclosed by brackets "[]" replaced with your own identifying information:
#
# Portions Copyright (c) [yyyy] [name of copyright owner]. All rights reserved.
#
# CDDL HEADER END
#

#
# Copyright (c) 2013--2018, Regents of the University of Minnesota.
# All rights reserved.
#
# Contributors:
#    Ryan S. Elliott
#

# check for errors

if test $# -lt 4; then
  printf "usage: %s packagename major minor patch [prerelease]\n" $0
  exit
fi

# DO IT

cd "`git rev-parse --show-toplevel`"

packagename=$1
major=$2
minor=$3
patch=$4
versionstring=v${major}.${minor}.${patch}
if test $# -ge 5; then
  prerelease=$5
  prerelease_w_space=" ${prerelease}"
  versionstring=${versionstring}-${prerelease}
else
  prerelease=""
  prerelease_w_space=""
fi
fullpackagename=${packagename}-${versionstring}

# check for more errors

if test -e "${fullpackagename}"; then
  printf "File '${fullpackagename}' exits! exiting...\n"
  exit
fi

if test $major -ne `grep VERSION_MAJOR Makefile.Version | sed -e 's/VERSION_MAJOR[[:space:]]*=[[:space:]]*//'`; then
  printf "Major version mismatch with Makefile.Version\n";
  printf "exiting...\n";
  exit
fi
if test $minor -ne `grep VERSION_MINOR Makefile.Version | sed -e 's/VERSION_MINOR[[:space:]]*=[[:space:]]*//'`; then
  printf "Minor version mismatch with Makefile.Version\n";
  printf "exiting...\n";
  exit
fi
if test $patch -ne `grep VERSION_PATCH Makefile.Version | sed -e 's/VERSION_PATCH[[:space:]]*=[[:space:]]*//'`; then
  printf "Patch version mismatch with Makefile.Version\n";
  printf "exiting...\n";
  exit
fi

git clone . $fullpackagename

cd $fullpackagename

versionfile=${packagename}.release.info
printf "This package ($fullpackagename) created from commit\n"                           >  $versionfile
printf "\n"                                                                              >> $versionfile
git rev-parse HEAD                                                                       >> $versionfile
printf "\n"                                                                              >> $versionfile
printf "of the kim-api git repository\n"                                                 >> $versionfile
printf "By `git config user.name` (`git config user.email`) on `date`.\n"                >> $versionfile

rm -rf '.git'

ed Makefile.Version <<EOF
,s/^VERSION_PRERELEASE.*/VERSION_PRERELEASE =${prerelease_w_space}/
,s/^VERSION_BUILD_METADATA.*/VERSION_BUILD_METADATA = \$(KIM_COMPILERSUITE).\$(KIM_SYSTEMLINKER).\$(KIM_SYSTEMARCH).\$(KIM_LINK)/
w
EOF

rm -f '.travis.yml'
rm -rf script
rm -f `find . -name ".gitignore"`

for fl in `grep -l -r "kim-api\.git repository" *`; do
   ed $fl <<EOF
,s/kim-api\.git repository/$fullpackagename package/g
w
EOF
done

cd ./documentation

make pdf
rm -rf "./latex"

cd ../..

tar cJvf $fullpackagename.txz $fullpackagename

rm -rf "$fullpackagename"
