#!/bin/sh
#
#
# This script is used to make a Bacula rescue release
#   rescue
# It writes in the current directory, so we recommend that you
#  do not run it in the Bacula source or Git directory, but
#  in a special release directory.
#
# Commit all changes, export the release, make a release tar.
#
. ./functions

cwd=`pwd`
reltype=release
tagbase=Release-
base_pkg=bacula-rescue
base_dir=rescue
remote=$2
branch=$3
ver=$4

if [ $# != 4 ] ; then
  echo "Need $0 <rescue-source-directory> <remote-name> <branch> <version>"
  echo "e.g. $0 rescue-source bee Branch-4.0 4.0.1"
  exit 1
fi
cd $1/rescue
check_return_code "$0: Directory $1/rescue does not exist"

current=`git branch | awk '/*/ { print $2 }'`
src=`pwd` 
git checkout ${branch}
check_return_code "$0: Checkout of branch ${branch} in ${src} failed."

git pull ${remote} ${branch}
fulltag=$tagbase$ver
echo " "
echo "$0: Making $reltype for $base_pkg-$ver ..."
echo " "
echo "OK?  ctl-c to stop"
read a

git tag -d ${fulltag} 2>/dev/null 1>/dev/null
echo "Creating new tag -- $fulltag"
git tag $fulltag
check_return_code "$0: Non-zero return status from git tag"
echo "Create Tag $fulltag done"
cd ${cwd}
rm -rf $base_pkg-$ver $base_pkg-$ver.tar.gz $base_pkg-$ver.tar.gz.sig
rm -rf $fulltag
cd ${src}
git archive --format=tar --prefix=$base_pkg-$ver/ $fulltag | gzip >${cwd}/$base_pkg-$ver.tar.gz
check_return_code "$0: Non-zero return status from Git"
echo "Exported release into ${cwd}/$base_pkg-$ver.tar.gz"
if [ a$push != ano ]; then
   cd ${src}
   git push ${remote} ${branch}
#  git push ${remote} tag ${fulltag}
   echo "Pushed ${remote}"
fi
cd ${src}
git checkout ${current}

cd ${cwd}
./sign $base_pkg-$ver.tar.gz
ls -l $base_pkg-$ver.tar.*
exit 0
