#!/bin/bash -e

WORK=$(pwd)
TOP=$(cd `dirname $0` ; pwd)
. $TOP/bloom_util.sh

LONG_USAGE=$(/bin/echo <<EOF
usage: git bloom-import-upstream
EOF
)
USAGE=$LONG_USAGE

. "$(git --exec-path)/git-sh-setup"

require_work_tree

_track_all

if ! git show-ref refs/heads/bloom >/dev/null
then
  if git show-ref refs/heads/catkin >/dev/null
  then
    # Rename old catkin refs to bloom
    git branch -m catkin bloom
    # rename the file
    git checkout bloom
    git mv catkin.conf bloom.conf
    sed -i 's/\[catkin\]/\[bloom\]/g' bloom.conf
  else
    bailout "This does not appear to be a bloom release repo. Please initialize it first using:
    git bloom-set-upstream <UPSTREAM_VCS_URL> <VCS_TYPE>"
  fi
fi

(set -x && git checkout bloom)
UPSTREAM_REPO=$(git config -f bloom.conf bloom.upstream)
UPSTREAM_TYPE=$(git config -f bloom.conf bloom.upstreamtype)
status "upstream repo: ${boldon}$UPSTREAM_REPO${reset}"
status "upstream type: ${boldon}$UPSTREAM_TYPE${reset}"

if [ $UPSTREAM_TYPE = "git" ] ; then
    status "Verifying a couple of things about the upstream git repo"
    assert_is_remote_git_repo $UPSTREAM_REPO
    assert_is_not_gbp_repo $UPSTREAM_REPO
fi

UPSTREAM_CLONE=$TMPDIR/upstream
repo_clone $UPSTREAM_TYPE $UPSTREAM_REPO $UPSTREAM_CLONE
if [ -f $UPSTREAM_CLONE/stack.xml ]; then
    read_stack_xml $UPSTREAM_CLONE/stack.xml
else
    read_stack_xml stack.xml
fi

status "Upstream's stack.xml has version ${boldon}$VERSION_FULL${reset}"
status "Upstream's name is ${boldon}$PACKAGE_NAME${reset}"

PACKAGE_NAME=$(/bin/echo $PACKAGE_NAME | tr _ -)
TARBALL_PREFIX=$TMPDIR/$PACKAGE_NAME-$VERSION_FULL
TARBALL=$TARBALL_PREFIX.tar.gz
(
    # set -x
    cd $UPSTREAM_CLONE
    status "Cloning tag $VERSION_FULL"
    repo_export $UPSTREAM_TYPE $UPSTREAM_CLONE $TARBALL_PREFIX $VERSION_FULL
    exit
)

LASTTAG=$(git for-each-ref --sort='*authordate' --format='%(refname:short)' refs/tags/upstream | tail -1)
if [ "$LASTTAG" = "" ]
then
    status "This is the first release into this repo."
    GBP_MAJOR=$VERSION_MAJOR
    GBP_MINOR=$VERSION_MINOR
    GBP_PATCH=$VERSION_PATCH
else
    status "The latest upstream tag in the release repo is ${boldon}$LASTTAG${reset}"
    extract_gbp_upstream_version $LASTTAG
    if dpkg --compare-versions $VERSION_FULL le $GBP_MAJOR.$GBP_MINOR.$GBP_PATCH
    then
      bailout "Version discrepency:
    The upstream version, $VERSION_FULL, must be greater than the previous
    release version, $GBP_MAJOR.$GBP_MINOR.$GBP_PATCH.

    Upstream must rerelease or you must fix your release repo.
"
    fi
fi

(
    set -e
    #clone the working dir into the temp dir for convenience.
    TWORK=$TMPDIR/release_$PACKAGE_NAME
    git clone -l $WORK $TWORK

    cd $TWORK

    _track_all

    if ! git branch | grep upstream
    then
        status "${boldon}No upstream${reset}... creating an initial upstream branch."
        git symbolic-ref HEAD refs/heads/upstream
        rm -f .git/index
        git clean -dfx
        git commit --allow-empty -m "Initial upstream branch"
    fi

    git checkout -f master
    if ! which git-import-orig ; then
        bailout "can't find git-import-orig... did you install git-buildpackage?"
    fi
    ( set -x; git import-orig $TARBALL )
    git push --all
    git push --tags
)

status "I'm happy.  You should be too."

exit 0
