#! /bin/sh
#
#   Copyright (c) 2015 Nat! - Mulle kybernetiK
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions are met:
#
#   Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
#   Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
#   Neither the name of Mulle kybernetiK nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.


#
# This is the main user interface to mulle-bootstrap
# sorta like git
#
BOOTSTRAP_SUBDIR=.bootstrap

PATH="/usr/local/libexec/mulle-bootstrap:$PATH"
export PATH

#
# simple option handling
#
if [ "$1" = "-n" ]
then
   MULLE_BOOTSTRAP_DRY_RUN=YES
   export MULLE_BOOTSTRAP_DRY_RUN
   shift
fi

if [ "$1" = "-v" ]
then
   if [ -z "${MULLE_BOOTSTRAP_TRACE}" ]
   then
      MULLE_BOOTSTRAP_TRACE=VERBOSE
      export MULLE_BOOTSTRAP_TRACE
   fi
   GITFLAGS="-v"
   export GITFLAGS
   shift
else
   if [ "$1" = "-V" ]
   then
      if [ -z "${MULLE_BOOTSTRAP_TRACE}" ]
      then
         MULLE_BOOTSTRAP_TRACE=ALL
         export MULLE_BOOTSTRAP_TRACE
      fi
      GITFLAGS="-v"
      export GITFLAGS
      shift
   else
      if [ "$1" = "-s" ] # silent
      then
         MULLE_BOOTSTRAP_TRACE=
         MULLE_BOOTSTRAP_TERSE=YES
         export MULLE_BOOTSTRAP_TERSE
         GITFLAGS="-q"
         export GITFLAGS
         shift
      fi
   fi
fi


usage()
{
   cat <<EOF
usage: mulle-bootstrap [-n][-v|-s] [command] [--help]
       version 0.6

 Specify the -h or --help option after each command to get more help.

 Flags (-n must be first, if present)
   -n        : do nothing creative or destructive
   -v        : be more verbose (-V even more verbose)
   -s        : be more terse

 Common user commands:
   bootstrap : does fetch and build recursively [default]
   nomagic   : does fetch and build, but not recursively
   clean     : cleans mulle-bootstrap produced files.

 Common maintainer commands:
   init      : initializes a bootstrap project
   xcode     : sets up the xcodeproj settings
   tag       : tag this project and fetched repositories

 Specialized commands:
   build     : builds fetched repositories
   fetch     : fetches required repositories recursively.
   update    : updates repositories (pull).
EOF
}


check_and_usage_and_help()
{
   cat <<EOF
usage: bootstrap
   There is nothing more to it.
EOF
}


command=${1:-"bootstrap"}
shift

. mulle-bootstrap-warn-scripts.sh



bootstrap()
{
   if [ "$1" = "-h" -o "$1" = "--help" ]
   then
       check_and_usage_and_help >&2
       exit 1
   fi

   #
   # this is run when checking mulle-project itself out
   #
   if [ -f mulle-bootstrap-local-environment.sh -a -x install.sh ]
   then
      . install.sh "$@"
      export MULLE_BOOTSTRAP=mulle-bootstrap
   else
      warn_scripts "${BOOTSTRAP_SUBDIR}"  || exit 1
   fi

   if [ "${DONT_RECURSE}" = "" ]
   then
      mulle-bootstrap-fetch.sh || exit 1
   else
      mulle-bootstrap-fetch.sh nonrecursive  || exit 1
   fi
   mulle-bootstrap-build.sh  || exit 1
}


main()
{

   if [ ! -d "${BOOTSTRAP_SUBDIR}" -a "${command}" != "init" ]
   then
      echo "there is no ${BOOTSTRAP_SUBDIR} here, can't continue" >&2
      exit 1
   fi

   case "${command}" in
      bootstrap)
         bootstrap "$@" || exit 1
         ;;

      nomagic)
         DONT_RECURSE=YES bootstrap "$@" || exit 1
         ;;

      build)
         mulle-bootstrap-build.sh "$@" || exit 1
         ;;

      clean)
         mulle-bootstrap-clean.sh "$@" || exit 1
         ;;

      fetch)
         mulle-bootstrap-fetch.sh "$@" || exit 1
         ;;

      update)
         COMMAND="update" mulle-bootstrap-fetch.sh "$@" || exit 1
         ;;

      init)
         mulle-bootstrap-init.sh "$@" || exit 1
         ;;

      setup-xcode|xcode)
         mulle-bootstrap-xcode.sh "$@"|| exit 1
         ;;

      tag)
         mulle-bootstrap-tag.sh "$@" || exit 1
         ;;

      *)
         usage >&2
         exit 1
         ;;
      esac
}

main "$@"