#!/bin/sh

BINOC_SYSTEM=`uname | tr [:upper:] [:lower:]`
BINOC_PYTHON=`which python2.7 2>/dev/null`
BINOC_GIT=`which git 2>/dev/null`
BINOC_CURL=`which curl 2>/dev/null`
BINOC_PLATFORM=./platform
BINOC_CALENDAR=./calendar
BINOC_MACH=$BINOC_PLATFORM/mach
BINOC_CONFIG_GUESS=`./platform/build/autoconf/config.guess 2>/dev/null`

# =============================================================================

if [ -z "$BINOC_PYTHON" ]; then
  printf "We could not find Python 2.7 which is required for just about everything!\n"
  exit 1
fi

# =============================================================================

# Determin the current OS
# This will also be exported so it can be picked up by .mozconfig
if [[ "$BINOC_SYSTEM" == "mingw32_nt-"* ]]; then
  BINOC_SYSTEM=windows
  
  if [ "$BINOC_CONFIG_GUESS" == "x86_64-pc-mingw32" ]; then
    BINOC_CONFIG_GUESS=win64
  else
    BINOC_CONFIG_GUESS=win32
  fi
fi

export BINOC_SYSTEM=$BINOC_SYSTEM
export BINOC_CONFIG_GUESS=$BINOC_CONFIG_GUESS

# =============================================================================

# Determin the git branches or commits for use in the .mozconfig objdir path
# If there is no git or you aren't building in a git repository then the value
# will be blank. This is ok.
if [ -n "$BINOC_GIT" ]; then
  BINOC_COMM_BRANCH=`"${BINOC_GIT}" symbolic-ref --short HEAD 2>/dev/null | tr [:upper:] [:lower:]` 
  BINOC_PLATFORM_BRANCH=`cd ${BINOC_PLATFORM} && "${BINOC_GIT}" symbolic-ref --short HEAD 2>/dev/null | tr [:upper:] [:lower:]`
  BINOC_CALENDAR_BRANCH=`cd ${BINOC_CALENDAR} && "${BINOC_GIT}" symbolic-ref --short HEAD 2>/dev/null | tr [:upper:] [:lower:]`

  # if we don't have a comm branch then don't even bother
  if [ -n "$BINOC_COMM_BRANCH" ]; then
    # If we aren't on the tip of a branch try getting the platform commit short sha
    if [ -z "$BINOC_PLATFORM_BRANCH" ]; then
      BINOC_PLATFORM_BRANCH=`cd ${BINOC_PLATFORM} && "${BINOC_GIT}" rev-parse --short HEAD 2>/dev/null`
    fi

    # See if the platform branch or commit sha if not just forget it
    if [ -n "$BINOC_PLATFORM_BRANCH" ]; then
      export BINOC_GIT_BRANCH=$BINOC_COMM_BRANCH-$BINOC_PLATFORM_BRANCH
    fi
  fi
fi

# =============================================================================

# This will process the commands given to the stub. If there is no match then
# just pass it to platform (real) mach
if [ "$1" == "release" ]; then
  # We are going to assume you want to build, package, and generate a mar
  $BINOC_MACH build
  $BINOC_MACH package

  # Windows also generates an NSIS installer and that is required before mar
  if [ "$BINOC_SYSTEM" == "windows" ]; then
    $BINOC_MACH installer
  fi

  $BINOC_MACH mar
elif [ "$1" == "localbuild" ]; then
  # This builds and stages the application in dist/MOZ_APP_NAME but does not
  # actually generate an archive or any of the other stuff
  $BINOC_MACH build
  $BINOC_MACH stage
elif [[ "$1" == "superclobber" && -d "../.obj" ]]; then
  printf "Removing all object directories in ../.obj\n"
  rm -rf ../.obj/*
elif [ "$1" == "version" ]; then
  # This will execute version2k.py and pass any remaining args to it
  $BINOC_PYTHON ./build/version2k.py ${@:2}
elif [ "$1" == "checkout" ] && [ -n "$BINOC_GIT" ]; then
  printf "binoc-central:\n"

  if [ "$2" == "absolute-trunk" ]; then
      "$BINOC_GIT" checkout TRUNK
  elif [ "$2" == "release" ]; then
      "$BINOC_GIT" checkout RELEASE
  elif [ -n "$2" ]; then
    printf "Please use git directly for arbitrary branch switching.\n"
    exit 1
  else
    _commBranch=`git rev-parse --abbrev-ref HEAD`
    if [ "$_commBranch" != "HEAD" ]; then 
      printf "On branch $_commBranch\n"
    else
      printf "Not currently on a branch. Please use git directly or 'mach checkout (release|absolute-trunk)'.\n"
      exit 1
    fi
  fi

  "$BINOC_GIT" pull

  if [ ! -f $BINOC_PLATFORM/CLOBBER ]; then
    "$BINOC_GIT" submodule init platform calendar
  fi

  if [ "$2" == "absolute-trunk" ]; then
    printf "\nUnified XUL Platform:\n"
    cd $BINOC_PLATFORM
    if [ "$BINOC_PLATFORM_BRANCH" != "master" ]; then
      "$BINOC_GIT" checkout master
    fi
    "$BINOC_GIT" pull

    cd ../$BINOC_CALENDAR
    printf "\nCalendar:\n"
    if [ "$BINOC_CALENDAR_BRANCH" != "trunk" ]; then
      "$BINOC_GIT" checkout TRUNK
    fi
    "$BINOC_GIT" pull
  else
    "$BINOC_GIT" submodule update -f
  fi
elif [ "$1" == "reset" ] && [ -n "$BINOC_GIT" ]; then
  if [ "$2" == "comm" ]; then
    printf "binoc-central:\n"
    "$BINOC_GIT" reset --hard
  elif [ "$2" == "platform" ]; then
    printf "Unified XUL Platform:\n"
    cd $BINOC_PLATFORM
    "$BINOC_GIT" reset --hard
  elif [ "$2" == "calendar" ]; then
    printf "Calendar:\n"
    cd $BINOC_CALENDAR
    "$BINOC_GIT" reset --hard
  else
    printf "Reset what?\n"
  fi
elif [ "$1" == "webpatch" ] && [ -n "$BINOC_GIT" ] && [ -n "$BINOC_CURL" ]; then
  if [ "$2" == "platform" ]; then
    printf "Unified XUL Platform:\n"
    cd $BINOC_PLATFORM
  elif [ "$2" == "calendar" ]; then
    printf "Calendar:\n"
    cd $BINOC_CALENDAR
  elif [ "$2" == "comm" ]; then
    printf "binoc-central:\n"
  else
    printf "Patch what?\n"
    exit 1
  fi

  if [ -z "$3" ]; then
    printf "Patch with what?"
    exit 1
  else
    if [[ "$3" == *"github.com"* ]] || [[ "$3" == *"repo.palemoon.org"* ]]; then
      _patch=$3.patch
    else
      _patch=$3
    fi
  fi

  $BINOC_CURL -L $_patch | "$BINOC_GIT" apply --reject
else
  # We don't know what the command is but real-mach might so just pass
  # all the args to it
  $BINOC_MACH $@
fi

