#!/bin/bash
#
# Usage
#
function usage () {
  echo "Usage: $0 [options] version"
  echo "where version is the version id and option is one of"
  echo " -h --help            this help"
  echo " -b --build FILE      Build source archive base filename"
  echo " -n --skip-version    Skip version check on fpc dir"
  echo " -s --src FILE        FPC Source archive base filename"
  echo " -d --doc FILE        Documentation source archive base filename"
  echo " -t --tree-ish TREE   tag/branch to use (HEAD by default)"
}
#
# Usage
#
while test $# != 0
do  
  f=$1
  case $f in
  -h|--help) usage
      exit 0;;
  -n|--skip-version)
     SKIPVERSIONCHECK=YES;;
  -s|--src)
     shift
     PACKNAME=$1;;
  -b|--build)
     shift
     BUILDNAME=$1;;
  -d|--doc)
     shift
     DOCNAME=$1;;
  -t|--tree-ish)
     shift
     TREE=$1;;
  *) VERSION=$1;;
  esac
  shift
done
#
# Sanity checks
#
if [ ! -d fpcdocs ]; then
  echo "No fpcdocs found. Execute this in the fpcbuild repo root dir"
  exit 1
fi

if [ ! -d fpcsrc ]; then
  echo "No fpcsrc found. Execute this in the fpcbuild repo root dir"
  exit 1
fi

if [ -z "$VERSION" ]; then
  echo "Error: Missing version"
  usage
  exit 1
fi
#
# Check source version
#
grep "version=$VERSION" fpcsrc/Makefile.fpc >/dev/null  2>&1
if [ "$?" = "0" ]; then
  echo "Source version check OK"  
else  
  echo "Version $VERSION not detected in fpcsrc/Makefile.fpc."
  if [ "$SKIPVERSIONCHECK" != 'YES' ]; then
    echo "Aborting. supply the -n command to skip this check"
    exit 1
  else
    echo "Ignoring wrong source version"  
  fi     
fi
#
# Check build version
#
grep "version=$VERSION" Makefile.fpc >/dev/null  2>&1
if [ "$?" = "0" ]; then
  echo "Build repo version check OK"  
else  
  echo "Version $VERSION not detected in Makefile.fpc."
  if [ "$SKIPVERSIONCHECK" != 'YES' ]; then
    echo "Aborting. supply the -n command to skip this check"
    exit 1
  else
    echo "Ignoring wrong build repo version"  
  fi     
fi
#
# Check documentation version
#  
grep "$VERSION" fpcdocs/fpc.sty >/dev/null  2>&1
if [ "$?" = "0" ]; then
  echo "Documentation version check OK"  
else  
  echo "Version $VERSION not detected in fpcdocs/fpc.sty. Aborting."
  if [ "$SKIPVERSIONCHECK" != 'YES' ]; then
    echo "Aborting. Supply the -n command to skip this check"
    exit 1
  else
    echo "Ignoring wrong documentation version"  
  fi
fi

#
# Set some defaults
#
if [ -z "$PACKNAME" ]; then
  PACKNAME=fpc-${VERSION}.source
fi

if [ -z "$DOCNAME" ]; then
  DOCNAME=docs-${VERSION}.source
fi

if [ -z "$BUILDNAME" ]; then
  BUILDNAME=fpcbuild-${VERSION}.source
fi

if [ -z "$TREE" ]; then
  TREE=HEAD
fi

if [ -z "$SRCPREFIX" ]; then
  SRCPREFIX=fpc-${VERSION}
fi

if [ -z "$DOCPREFIX" ]; then
  DOCPREFIX=fpcdocs-${VERSION}
fi

if [ -z "$BUILDPREFIX" ]; then
  BUILDPREFIX=fpcbuild-${VERSION}
fi
#
# Start archiving. Note that the archives are fake-root (root/root owner).
# Every archive unpacks into a single directory. The .zip archives use CRLF
# line endings for text files, the .tar.gz archives keep LF.
#
echo "Creating source archive $PACKNAME.tar.gz"
git -C fpcsrc archive --format=tar.gz --prefix=$SRCPREFIX/ -o ../$PACKNAME.tar.gz $TREE

echo "Creating source archive $PACKNAME.zip"
git -C fpcsrc -c core.autocrlf=true archive --format=zip --prefix=$SRCPREFIX/ -o ../$PACKNAME.zip $TREE

echo "Creating documentation source archive $DOCNAME.tar.gz"
git -C fpcdocs archive --format=tar.gz --prefix=$DOCPREFIX/ -o ../$DOCNAME.tar.gz $TREE

echo "Creating documentation source archive $DOCNAME.zip"
git -C fpcdocs -c core.autocrlf=true archive --format=zip --prefix=$DOCPREFIX/ -o ../$DOCNAME.zip $TREE
#
# The build archive contains the fpcsrc and fpcdocs submodules. git archive
# does not descend into submodules, so the tree is assembled in a temp dir.
# Argument 1 is the value for core.autocrlf.
#
BASEDIR=`pwd`
WORKDIR=`mktemp -d`
trap "rm -rf $WORKDIR" EXIT

function buildtree () {
  rm -rf $WORKDIR/$BUILDPREFIX
  mkdir -p $WORKDIR/$BUILDPREFIX
  git -c core.autocrlf=$1 archive --format=tar $TREE | tar -C $WORKDIR/$BUILDPREFIX -xf -
  mkdir -p $WORKDIR/$BUILDPREFIX/fpcsrc $WORKDIR/$BUILDPREFIX/fpcdocs
  git -C fpcsrc -c core.autocrlf=$1 archive --format=tar $TREE | tar -C $WORKDIR/$BUILDPREFIX/fpcsrc -xf -
  git -C fpcdocs -c core.autocrlf=$1 archive --format=tar $TREE | tar -C $WORKDIR/$BUILDPREFIX/fpcdocs -xf -
}

echo "Creating build source archive $BUILDNAME.tar.gz"
buildtree false
rm -f $BASEDIR/$BUILDNAME.tar.gz
tar -C $WORKDIR --owner=0 --group=0 -czf $BASEDIR/$BUILDNAME.tar.gz $BUILDPREFIX

echo "Creating build source archive $BUILDNAME.zip"
buildtree true
rm -f $BASEDIR/$BUILDNAME.zip
cd $WORKDIR && zip -qrD9 $BASEDIR/$BUILDNAME.zip $BUILDPREFIX
cd $BASEDIR

rm -rf $WORKDIR
#
# That's all, folks !
#
