#!/bin/bash
#   This file is part of nexdatas - Tango Server for NeXus data writer
#
#    Copyright (C) 2017-2017 DESY, Jan Kotanski <jkotan@mail.desy.de>
#
#    nexdatas is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    nexdatas is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with nexdatas.  If not, see <http://www.gnu.org/licenses/>.
#

for i in "${@:1}"
do
    if [[ $i == -* ]] ; then
	case $i in
	    -h=*|--help=*)
		HELP=YES
		;;
	    -e=*|--email=*)
		EMAIL="${i#*=}"
		;;
	    -f=*|--fullname=*)
		FULLNAME="${i#*=}"
		;;
	    -u=*|--user=*)
		export USER="${i#*=}"
		;;
	    -r=*|--release=*)
		export RELEASE="${i#*=}"
		;;
	    -ne|--nodebenv)
		export NODEBENV=YES
		;;
	    -p|--patch)
		export PATCH=YES
		;;
	    *)
		# unknown option
		;;
	esac
    else
	export PACKAGE=$1
    fi
done

DEFRELEASE=jessie

if [[ ! -z $HELP ]] && [[ -z $PACKAGE ]] ; then
    echo "Error: PACKAGE variable is not defined"
fi
if [[ ! -z $HELP ]] || [[ -z $PACKAGE  ]] ; then
    echo "usage: pidpclone <DEB-PACKAGE-NAME> [-p|--patch] [-u=<USER>] [-h|--help]"
    echo "                  [-e=<DEBEMAIL>] [-f=<DEBFULLNAME>] [-ne|--nodebenv] "
    echo ""
    echo "clone the debian package repository from stash into the local directory"
    echo "and start the patching procedure if the --patch option is given"
    echo "(see also pidpbuild)"
    echo ""
    echo "possible arguments:"
    echo "  DEB-PACKAGE-NAME      debian package name (mandatory)"
    echo ""
    echo "optional arguments:"
    echo "  -h, --help            show this help message and exit"
    echo "  -p, --patch           start the patching procedure"
    echo "  -u=, --user=          stash user name"
    echo "                          (default: the local user name)"
    echo "  -ne, --nodebenv       don't set default DEBFULLNAME and DEBEMAIL"
    echo "  -f=, --fullname=      debian fullname of the gpg signature,"
    echo "                          i.e. DEBFULLNAME"
    echo "  -e=, --email=         debian email of the gpg signature,"
    echo "                          i.e.  DEBEMAIL"
    echo ""
    echo "examples:"
    echo "  pidpclone python-nxstools-extras-p09 --patch"
    echo "  pidpclone python-nxstools-extras-p06"
    echo ""
else
    if [ -z $NODEBENV ] ; then
        export DEBEMAIL="jankotan@gmail.com"
        export DEBFULLNAME="Jan Kotanski (jkotan)"
    fi
    if [[ ! -z $FULLNAME ]] ; then
        export DEBFULLNAME=$FULLNAME
    fi
    if [[ ! -z $EMAIL ]] ; then
        export DEBEMAIL=$EMAIL
    fi
    if [ -z $USER ] ; then
	export USER=$(whoami)
    fi
    if [ -z $RELEASE ] ; then
	export RELEASE=$DEFRELEASE
    fi
    git clone https://$USER@stash.desy.de/scm/pidp/$PACKAGE.git
    cd $PACKAGE
    git fetch --all
    git checkout -b upstream/$RELEASE origin/upstream/$RELEASE
    git checkout -b pristine-tar origin/pristine-tar
    git checkout debian/$RELEASE 2> /dev/null
    if ! [[ $? -eq 0 ]]; then
	git checkout -b debian/$RELEASE origin/debian/$RELEASE
    fi
    export PIDTOKILL=$$
    if [ -z $PATCH ] ; then
	echo ""
	echo "1) Edit the debian/ configuration"
	echo "2) Commit your changes"
	echo "3) Execute pidpbuild"
    else
	gbp pq import
	export IMPORT=YES
	export PIDTOKILL=$$
	echo ""
	echo "1) Edit the source files"
	echo "2) Commit your changes"
	echo "3) Execute pidpbuild"
    fi
    exec bash
fi