#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x

git_archive_all() {
  APP=$1; REV=$2
  TMP_WORK_DIR=$(mktemp -d)
  chmod 755 $TMP_WORK_DIR
  unset GIT_DIR GIT_WORK_TREE
  pushd $TMP_WORK_DIR > /dev/null
  git clone -q $DOKKU_ROOT/$APP $TMP_WORK_DIR &> /dev/null
  git config advice.detachedHead false
  git checkout $REV &> /dev/null
  git submodule update --init --recursive &> /dev/null
  find -name .git -prune -exec rm -rf {} \; > /dev/null
  tar c .
  rm -rf $TMP_WORK_DIR > /dev/null
}

case "$1" in
  git-hook)
    APP=$2

    while read oldrev newrev refname
    do
      # Only run this script for the master branch. You can remove this
      # if block if you wish to run it for others as well.
      if [[ $refname = "refs/heads/master" ]] ; then
        dokku git-build $APP $newrev
      else
        echo $'\e[1G\e[K'"-----> WARNING: deploy did not complete, you must push to master."
        echo $'\e[1G\e[K'"-----> for example, try 'git push <dokku> ${refname/refs\/heads\/}:master'"
      fi
    done
    ;;

  git-upload-pack)
    APP="$(echo $2 | perl -pe 's/(?<!\\)'\''//g' | sed 's/\\'\''/'\''/g')"
    pluginhook git-pre-pull $APP
    cat | git-upload-pack "$DOKKU_ROOT/$APP"
    pluginhook git-post-pull $APP
    ;;

  git-build)
    APP="$2"; APP_BUILD_LOCK="$DOKKU_ROOT/$APP/.build.lock"
    APP_BUILD_LOCK_MSG="$APP is currently being deployed or locked. Waiting..."
    [[ $(flock -n "$APP_BUILD_LOCK" true &>/dev/null ; echo $?) -ne 0 ]] && echo "$APP_BUILD_LOCK_MSG"

    shift 1
    flock -o "$APP_BUILD_LOCK" dokku git-build-locked "$@"
    ;;

  git-build-locked)
    APP="$2"
    if [[ $# -ge 3 ]];then
      REF="$3"
    else
      REF=$(< "$DOKKU_ROOT/$APP/refs/heads/master")
    fi
    git_archive_all $APP $REF | dokku receive $APP | sed -u "s/^/"$'\e[1G'"/"
    ;;

  git-*)
    APP="$(echo $2 | perl -pe 's/(?<!\\)'\''//g' | sed 's/\\'\''/'\''/g')"
    APP_PATH=$DOKKU_ROOT/$APP

    if [[ $1 == "git-receive-pack" && ! -d "$APP_PATH/refs" ]]; then
      git init --bare $APP_PATH > /dev/null
      PRERECEIVE_HOOK="$APP_PATH/hooks/pre-receive"
      cat > $PRERECEIVE_HOOK <<EOF
#!/usr/bin/env bash
set -e; set -o pipefail;

cat | DOKKU_ROOT="$DOKKU_ROOT" dokku git-hook $APP
EOF
      chmod +x $PRERECEIVE_HOOK
    fi

    args=$*
    git-shell -c "$args"
    ;;

  help | git:help)
    cat
    ;;

  *)
    exit $DOKKU_NOT_IMPLEMENTED_EXIT
    ;;

esac
