#! /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=0.10.1

#
# 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" = "-a" ]
then
   MULLE_BOOTSTRAP_ANSWER="NO"
   export MULLE_BOOTSTRAP_ANSWER
   [ $# -eq 0 ] || shift
else
   if [ "$1" = "-y" ] #  basically auto symlink everything
   then
      MULLE_BOOTSTRAP_ANSWER="YES"
      export MULLE_BOOTSTRAP_ANSWER
      [ $# -eq 0 ] || shift
   fi
fi


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

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

   COPYMOVEFLAGS="-v"
   export COPYMOVEFLAGS
   GITFLAGS="-v"
   export GITFLAGS
   [ $# -eq 0 ] || shift
else
   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
   else
      if [ "$1" = "-V" ]
      then
         if [ -z "${MULLE_BOOTSTRAP_TRACE}" ]
         then
            MULLE_BOOTSTRAP_TRACE="ALL"
            export MULLE_BOOTSTRAP_TRACE
         fi

         COPYMOVEFLAGS="-v"
         export COPYMOVEFLAGS
         GITFLAGS="-v"
         export GITFLAGS
         [ $# -eq 0 ] || shift
      else
         if [ "$1" = "-s" ] # silent
         then
            MULLE_BOOTSTRAP_TRACE=
            MULLE_BOOTSTRAP_TERSE="YES"
            export MULLE_BOOTSTRAP_TERSE

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


usage()
{
   cat <<EOF
usage: mulle-bootstrap [-a|-y][-n][-v|-s|-t] [command] [--help]
 Flags (-n must be first, if present)
   -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 be more verbose (-V even more verbose), -s be more terse
               -t turns on shell tracing for development

 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
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.
   update    : updates repositories (pull).

 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
}


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


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

   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" -a \
      "${command}" != "version"  -a \
      "${command}" != "convert-pre-0.10" ]
   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
         ;;

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

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

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

      convert-pre-0.10)
         mulle-bootstrap-convert-pre-0.10.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
         ;;

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

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

main "$@"