#! /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.

VERSION=1.2
export VERSION

#
# 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
#
while :
do
   if [ "$1" = "-a" ]
   then
      MULLE_BOOTSTRAP_ANSWER="NO"
      export MULLE_BOOTSTRAP_ANSWER
      [ $# -eq 0 ] || shift
      continue
   fi

   if [ "$1" = "-y" ] #  basically auto symlink everything
   then
      MULLE_BOOTSTRAP_ANSWER="YES"
      export MULLE_BOOTSTRAP_ANSWER
      [ $# -eq 0 ] || shift
      continue
   fi


   if [ "$1" = "-n" ]
   then
      MULLE_BOOTSTRAP_DRY_RUN="YES"
      export MULLE_BOOTSTRAP_DRY_RUN
      [ $# -eq 0 ] || shift
      continue
   fi

   if [ "$1" = "-t" ]
   then
      MULLE_BOOTSTRAP_TRACE="1848"
      export MULLE_BOOTSTRAP_TRACE

      COPYMOVEFLAGS="-v"
      export COPYMOVEFLAGS
      GITFLAGS="-v"
      export GITFLAGS
      [ $# -eq 0 ] || shift
      continue
   fi

   if [ "$1" = "-V" ]
   then
      MULLE_BOOTSTRAP_VERBOSE_BUILD="YES"
      export MULLE_BOOTSTRAP_VERBOSE_BUILD

      [ $# -eq 0 ] || shift
      continue
   fi

   if [ "$1" = "-v" ]
   then
      if [ -z "${MULLE_BOOTSTRAP_TRACE}" ]
      then
         MULLE_BOOTSTRAP_TRACE="VERBOSE"
         export MULLE_BOOTSTRAP_TRACE
      fi

      COPYMOVEFLAGS="-v"
      export COPYMOVEFLAGS
      GITFLAGS="-v"
      export GITFLAGS
      [ $# -eq 0 ] || shift
      continue
   fi

   if [ "$1" = "-vv" ]
   then
      if [ -z "${MULLE_BOOTSTRAP_TRACE}" ]
      then
         MULLE_BOOTSTRAP_TRACE="FLUFF"
         export MULLE_BOOTSTRAP_TRACE
      fi

      COPYMOVEFLAGS="-v"
      export COPYMOVEFLAGS
      GITFLAGS="-v"
      export GITFLAGS
      [ $# -eq 0 ] || shift
      continue
   fi

   if [ "$1" = "-vvv" ]
   then
      if [ -z "${MULLE_BOOTSTRAP_TRACE}" ]
      then
         MULLE_BOOTSTRAP_TRACE="TRACE"
         export MULLE_BOOTSTRAP_TRACE
      fi

      COPYMOVEFLAGS="-v"
      export COPYMOVEFLAGS
      GITFLAGS="-v"
      export GITFLAGS
      [ $# -eq 0 ] || shift
      continue
   fi

   if [ "$1" = "-s" ] # silent
   then
      MULLE_BOOTSTRAP_TRACE=
      MULLE_BOOTSTRAP_TERSE="YES"
      export MULLE_BOOTSTRAP_TERSE

      GITFLAGS="-q"
      export GITFLAGS
      [ $# -eq 0 ] || shift
      continue
   fi

   break
done


usage()
{
   cat <<EOF
usage: mulle-bootstrap [-a|-y][-n][-v|-s|-t] [command] [--help]
 Flags
   -a -y     : default answer to questions (scripts wont be checked)
               -a (clone preferred) -y (symlink preferred)
   -n        : do nothing creative or destructive
   -v -s -t  : -v to be more verbose (-vv or -vvv for more verbosity)
               -s to be more terse
               -t turns on shell tracing for development
   -V        : show build commands

 Common user commands:
   clone     : git clone a remote repository and try to bootstrap and build it
   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
EOF

   if [ "`uname`" = 'Darwin' ]
   then
      cat <<EOF
   xcode     : sets up the xcodeproj settings
EOF
   fi

cat <<EOF
   tag       : tag this project and fetched repositories

 Specialized commands:
   build     : builds fetched repositories
   fetch     : fetches required repositories recursively. Use it after updating
               the repositories and embedded_repositories files.
   update    : updates repositories (pull).
   install   : install dependencies as system headers

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


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


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
      mulle-bootstrap-warn-scripts.sh "${BOOTSTRAP_SUBDIR}" || exit 1
   fi

   COMMAND="refresh" mulle-bootstrap-refresh.sh  || exit 1

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

   # do it again now!

   COMMAND="refresh" mulle-bootstrap-refresh.sh  || exit 1
   mulle-bootstrap-build.sh  || exit 1
}



main()
{
   local command

   command=${1:-"bootstrap"}
   [ $# -eq 0 ] || shift

   #
   # some commands only run when we have a .bootstrap folder
   #
   if [ ! -d "${BOOTSTRAP_SUBDIR}" ]
   then
      case "${command}" in
         bootstrap|nomagic|build|ibuild|refresh)
            echo "There is no ${BOOTSTRAP_SUBDIR} folder here, can't continue" >&2
            exit 1
         ;;
         dist-clean|fetch|update|setup-xcode|xcode|tag)
            echo "There is no ${BOOTSTRAP_SUBDIR} folder here, can't continue" >&2
            exit 1
         ;;
      esac
   fi

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

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

      # experimental stuff
      project)
         mulle-bootstrap-project.sh "$@" || exit 1
         ;;

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

      build)
         COMMAND="refresh" mulle-bootstrap-refresh.sh || exit 1
         COMMAND="${command}" mulle-bootstrap-build.sh "$@" || exit 1
         ;;

      refresh)
         COMMAND="${command}" mulle-bootstrap-refresh.sh || exit 1
         ;;

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

      dist-clean)
         COMMAND="refresh" mulle-bootstrap-refresh.sh || exit 1
         mulle-bootstrap-clean.sh "dist"  || exit 1
         ;;

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

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

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

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

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

      version)
         echo "${VERSION}" || exit 1
         ;;

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

main "$@"