#!/usr/bin/env bash

ARGV=("$@")


###
# http://stackoverflow.com/a/23342259
###
exe() { echo "\$ $@" ; "$@" ; }

CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $CWD

show_usage () {
  echo ""
  echo "Set up the building of libssts and clean up too."
  echo "  This is not ment as a replacment for bazaar or for meson, just a"
  echo "  \"kick-start\" for new developers... and makes branching easier."
  echo ""
  echo "  ./build.sh clean      -  Clean up the project and build files."
  echo "             cl"
  echo ""
  echo "  ./build.sh build      -  Create the build directory, run CMake (if"
  echo "             bl            needed) and build libssts."
  echo ""
  echo "  ./build.sh help       -  Show this helpful help text."
  echo "             -h"
  echo ""
  echo "  ./build.sh clean-tree -  clean the bzr three. reverting all and any"
  echo "             ct            changes made to the file tree since the last"
  echo "                           commit."
  echo ""
  echo "  ./build.sh commit     -  commit changes."
  echo "             cm"
  echo ""
  echo "  ./build.sh branch <name> - create a branch. This will cd up one level"
  echo "             br              and then do a branch. The name of the"
  echo "                             branch will be \"simpletypesystem_<name>\"."
  echo ""
  echo "  ./build.sh merge-trunk   - Commits your current changes and then pull"
  echo "             mt             merge from trunk. If the merge went smoothly"
  echo "                            it will procide to commit the merge with"
  echo "                            message \"* Merged trunk.\""
  echo ""
  echo " ./build.sh propose-merge  - Propese merge of current branch."
  echo "            pm"
}

do_build () {
  cd $CWD
  if ( ! -d "./build" )
  then
    exe mkdir ./build
  fi
  
  cd ./build
  
  if [ ! -f "build.ninja" ]
  then
    exe meson ..
  fi
  
  ninja
}

do_test () {
  if ( ! -d "./build" )
  then
    do_build
  fi
  
  cd $__CWD/build
  
  exe ninja test
  
}

do_clean () {
  if [ -d "./build" ]
  then
    exe rm -R ./build
  fi
}

do_clean_tree () {
  exe brz revert
  exe brz clean-tree 
}

do_commit () {
  exe brz commit
}


do_branch () {
  echo ${ARGV[1]}
  if [[ -z ${ARGV[1]} ]]
  then
    echo "Missing branch name."
    show_usage
    exit
  else
    #build up the branch name.
    NEW_BRANCH_NAME="simpletypesystem_"${ARGV[1]}
    CURRENT_WD=$(pwd)
    exe cd ..
    PARENT_WD=$(pwd)
    exe bzr branch $CURRENT_WD $PARENT_WD/$NEW_BRANCH_NAME
    if [[ -z $? ]]
    then
      echo ""
      echo "Something went wrong when trying to branch..."
    else
      echo ""
      echo "The directory $PARENT_WD/$NEW_BRANCH_NAME is ready. To go to that"
      echo "branch cd to it."
      echo ""
      echo "Remember to commit often and run"
      echo "$ bzr push --remember lp:~<lp-username>/simpletypesystem/${ARGV[1]}"
      echo "before you start hacking."
    fi
  fi
}

do_merge_trunk () {
  exe brz commit
  exe brz merge lp:simpletypesystem
  if [[ -z $? ]]
  then
    echo "Something went wrong with the merge! Plese solve these problems"
    echo "before you procide to commit again!"
  else
    exe commit -m "* Merged with trunk."
  fi
}

do_propose_merge () {
  exe brz lp-propose-merge
}

case ${ARGV[0]} in
     "help"|"-h") show_usage ;;
     "clean"|"cl") do_clean ;;
     "build"|"bl") do_build ;;
     "test"|"tt") do_test ;;
     "clean-tree"|"ct") do_clean_tree ;;
     "commit"|"cm") do_commit ;;
     "branch"|"br") do_branch ;;
     "merge-trunk"|"mt") do_merge_trunk ;;
     "propose-merge"|"pm") do_propose_merge ;;
     *) show_usage ;;
esac

cd $CWD

