#! /bin/bash -xe

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# This script is maintained at https://github.com/openSUSE/linuxrc-devtools
#
# If you're in another project, this is just a copy.
# You may update it to the latest version from time to time...
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

PATH=$PATH:utils

while true ; do
  if [ "$1" = "--name" ] ; then
    PACKAGE_NAME="$2"
    shift
    shift
    continue
  fi

  break
done

if [ -z "$PACKAGE_NAME" ] ; then
  PACKAGE_NAME=$(git config --get remote.origin.url | perl -pe 's#^.*/|\.git$##g')
fi

if [ -z "$PACKAGE_NAME" ] ; then
  echo no package name set
  exit 1
fi

VERSION=`git2log --version VERSION ; cat VERSION`
PACKAGE_PREFIX=$PACKAGE_NAME-$VERSION

mkdir -p package
rm -f package/*.tar.xz package/*.changes

git2log --changelog --format obs package/$PACKAGE_NAME.changes

if [ ! -d .git ] ; then
  echo no git repo
  exit 1
fi

git archive --prefix=$PACKAGE_PREFIX/ HEAD > package/$PACKAGE_PREFIX.tar

tar -r -f package/$PACKAGE_PREFIX.tar \
  --mode=0664 --owner=root --group=root \
  --mtime="`git show -s --format=%ci`" \
  --transform="s:^:$PACKAGE_PREFIX/:" VERSION
xz -f package/$PACKAGE_PREFIX.tar
