#!/bin/bash
#
#******************************************************************************
# bootstrap (Sequencer64)
#------------------------------------------------------------------------------
##
# \file       	bootstrap
# \library    	Sequencer64
# \author     	Chris Ahlstrom
# \date       	2015-09-10
# \update     	2020-02-16
# \version    	$Revision$
# \license    	$XPC_SUITE_GPL_LICENSE$
#
#     The above is modified by the following to remove even the mild GPL
#     restrictions:
#
#     Use this script in any manner whatsoever.  You don't even need to give
#     me any credit.
#
#     However, keep in mind the value of the GPL in keeping software and its
#     descendant modifications available to the community for all time.
#     runs the configure script by default.
#
#     This file provides the functionality most expect to find in an
#     autogen.sh script.
#
#------------------------------------------------------------------------------

#******************************************************************************
#  Provide a sane environment, just in case it is needed.
#------------------------------------------------------------------------------

LANG=C
export LANG
CYGWIN=binmode
export CYGWIN
export SEQ64_SCRIPT_EDIT_DATE="2020-02-16"
export SEQ64_LIBRARY_API_VERSION="0.96"
export SEQ64_LIBRARY_VERSION="$SEQ64_LIBRARY_API_VERSION.96.8"

#******************************************************************************
#  Version info
#------------------------------------------------------------------------------

if [ "$1" == "--version" ] || [ "$1" == "-v" ] ; then
   echo "Sequencer64 version $SEQ64_LIBRARY_VERSION $SEQ64_SCRIPT_EDIT_DATE"
   exit 0
fi

#******************************************************************************
#  Set up for local bootstrapping
#------------------------------------------------------------------------------

source strap_functions

if [ $? != 0 ] ; then
   echo "'source strap_functions' failed.  Aborting."
   exit 255
fi

checkdir Sequencer64

#******************************************************************************
#  Option settings
#------------------------------------------------------------------------------

DOBOOTSTRAP="yes"
DOALSAMIDI="no"
DOCLEAN="no"
DORELEASE="yes"
DODEBUG="no"
DOFULLCLEAN="no"
DOMINGW="no"
DOPORTMIDI="no"
DORTCLI="no"
DOSTATIC="no"
DOTESTONLY="no"
DOVERSION="no"
DOALSAMIDI="no"
DOPORTMIDI="no"
DOGTKMMUI="yes"
DOQT5UI="no"
LOGFILENAME=""
M4DIR="m4"

#******************************************************************************
#  Default boostrap build
#------------------------------------------------------------------------------

DORTMIDI="yes"
EXTRAFLAGS=""

#******************************************************************************
#  Help
#------------------------------------------------------------------------------

if [ "$1" == "--help" ] ; then

   cat << E_O_F
Usage: ./bootstrap [options]                                 (0.96.8-2020-02-16)

 Sets up GNU automake and libtool support for Sequencer64, and creates the
 'configure' script.  It can also full-clean the project, removing all generated
 files and directories.  It provides some options to save trouble passing long
 options to the configure script.

 --clean             Delete the usual derived files from the project.
 --full-clean        Delete all derived and configure-related files.  The
                     bootstrap script will need to be run again.
 -ed, --enable-debug Also configure for debugging, which also disables
 --enable-debug=gdb  shared libraries/libtool for easier debugging.
 --release, -er,     Also configure for release, to save a little time.  Sets up
   --enable-release  a 'silent' build, as well.  Note that configuring requires
                     either --debug or --release to be specified.
 --static            Do a static, release, "rtmidi" build.
 -am                 Configure to build sequencer64 (original ALSA version).
                     Implies --enable-alsamidi.  Deprecated.
 -pm                 Configure for seq64portmidi.  Implies --enable-portmidi.
 -rm                 Configure for seq64rtmidi (JACK version, the default).
                     Gtkmm-2.4 is the default user interface (Linux only).
 -qt                 Configure for Qt5 user-interface.
 -cli, -rc           Configure for seq64cli (command-line rtmidi version).
 --windows, -win     Configure for Windows.  Untested! Use qmake instead!
 --help              Show this help text.

 The default bootstraps the projects.  Then one runs 'configure' to
 set up for a release build.  A common option for 'bootstrap' is
 --enable-debug (-ed); see the note above.
E_O_F

   exit 1

fi

#******************************************************************************
#  Brute-force options loop
#------------------------------------------------------------------------------

if [ $# -ge 1 ] ; then

   while [ "$1" != "" ] ; do

      case "$1" in

         --no-bootstrap | -nb)
            DOBOOTSTRAP="no"
            ;;

         --clean | clean)
            DOBOOTSTRAP="no"
            DOCLEAN="yes"
            ;;

         --full-clean | --super-clean)
            DOBOOTSTRAP="no"
            DOFULLCLEAN="yes"
            DOCLEAN="yes"
            ;;

         --debug | -ed | --enable-debug | --ed)
            DODEBUG="yes"
            DORELEASE="no"
            ;;

         --release | -er | --enable-release | --er)
            DODEBUG="no"
            DORELEASE="yes"
            ;;

         --static)
            DORELEASE="yes"
            DORTMIDI="yes"
            EXTRAFLAGS+=" --enable-rtmidi --disable-shared --enable-static"
            ;;

         --alsamidi | -am | --am)
            DOALSAMIDI="yes"
            EXTRAFLAGS+=" --enable-alsamidi --disable-rtmidi"
            ;;

         --portmidi | -pm | --pm)
            DOPORTMIDI="yes"
            EXTRAFLAGS+=" --enable-portmidi --disable-rtmidi"
            ;;

         --rtmidi | -rm | --rm)
            DORTMIDI="yes"
            EXTRAFLAGS+=" --enable-rtmidi"
            ;;

         --qt | -qt)
            DOGTKMMUI="no"
            DOQT5UI="yes"
            EXTRAFLAGS+=" --enable-qt"
            ;;

         --cli | -cli | -rc)
            DORTCLI="yes"
            EXTRAFLAGS+=" --enable-cli"
            ;;

         --windows | -win | -w64)
            DORTMIDI="no"
            DOGTKMMUI="no"
            DORTCLI="no"
            DOQT5UI="yes"
            DOPORTMIDI="yes"
            DOMINGW="yes"
            EXTRAFLAGS=" --disable-rtmidi --enable-portmidi --enable-mingw --build=x86_64-pc-linux-gnu --host=x86_64-w64-mingw32"
            ;;

         --win32 | -w32)
            DORTMIDI="no"
            DORTCLI="yes"
            DOMINGW="yes"
            EXTRAFLAGS=" --disable-rtmidi --enable-cli --enable-mingw --build=i686-pc-linux-gnu --host=i686-w64-mingw32"
            ;;

         --version)
            DOVERSION="yes"
            DOBOOTSTRAP="no"
            ;;

         *)
            echo "? Unsupported bootstrap option; --help for more information" ;
            exit $EXIT_ERROR_NO_SUCH_OPTION
            ;;

      esac
      shift
   done
fi

if [ "$EXTRAFLAGS" == "" ] ; then
   EXTRAFLAGS="--enable-rtmidi"
fi

echo "EXTRAFLAGS = $EXTRAFLAGS"

#******************************************************************************
#  Implement the clean option.
#------------------------------------------------------------------------------
#
#     This goes well beyond "make clean" and "make distclean".  It cleans
#     out /everything/ that gets created by bootstrapping and building the
#     library and test application.
#
#------------------------------------------------------------------------------

if [ $DOCLEAN == "yes" ] ; then
   make clean 
   clean_cfgfiles
   clean_gnufiles
   clean_tempfiles
   clean_m4
   clean_qt5files

# Later, see about using clean_tree(), clean_project(), but safely.

   rm -f include/seq64-config.h
   echo "   Config, GNU, temp, m4, and Qt 5 generated files removed."
fi

if [ $DOFULLCLEAN == "yes" ] ; then

   rm -rf config
   rm -f include/config.h
   rm -f include/stamp-h1
   rm -f include/seq64-config.h
   rm -rf po
#  rm -f configure
   clean_debfiles
   echo "   All junk files removed."

fi

if [ $DOBOOTSTRAP == "yes" ] ; then

#************************************************************************
#  Set up GNU Autotools
#------------------------------------------------------------------------

   AUTOMAKE_BAD=no
   INSTALL_BAD=no
   ACVERSION=
   ACLOCAL=aclocal${ACVERSION}
   AUTOCONF=autoconf
   AUTOHEADER=autoheader
   AUTOMAKE=automake
   LIBTOOLIZE=libtoolize

# Exit if a simple command exits with a non-zero status.

   set -e

# After expanding a simple command, display PS4 and the command with its
# expanded arguments.  This setting makes any error messages too
# difficult to read:
#
# set -x

# Check Autoconf version and perform clean ups if all is well.

   if [ -x `which autoconf` ] ; then

      AC_VER=`autoconf --version | head -1 | sed 's/^[^0-9]*//'`
      AC_VER_MAJOR=`echo $AC_VER | cut -f1 -d'.'`
      AC_VER_MINOR=`echo $AC_VER | cut -f2 -d'.' | sed 's/[^0-9]*$//'`

      if [ "$AC_VER_MAJOR" -lt "2" ] ; then

         echo
         echo "Autoconf 2.13 or greater may be needed to build configure."
         echo "Edit the bootstrap file to ignore this test, if desired."
         echo
         exit $EXIT_ERROR_AUTOCONF_VERSION

      fi

      if [ "$AC_VER_MINOR" -lt "13" ] ; then

         echo
         echo "Autoconf 2.13 or greater may be needed to build configure."
         echo "Edit the bootstrap file to ignore this test, if desired."
         echo
         exit $EXIT_ERROR_AUTOCONF_VERSION_2

      fi

      if [ "$AC_VER_MINOR" -lt "50" ] ; then

         if [ -e configure.ac ] ; then
            if [ ! -e configure.in ] ; then
               ln -s configure.ac configure.in
            fi
         fi
         echo "Some warnings about cross-compiling are normal."

      else

         rm -f configure.in
         if [ -x configure ] ; then
            echo The Sequencer64 configure script already exists.  Replacing it.
         fi
      fi

   else

      cat << E_O_F

   The GNU autoconf application was not found.  This project requires GNU
   autoconf (and automake, and ac-autoconf-archive) in order to
   bootstrap itself.

E_O_F
      exit $EXIT_ERROR_AUTOCONF_VERSION_3
   fi

# Check for automake.

   amvers="none"
   if automake-1.8 --version >/dev/null 2>&1; then
      amvers="-1.8"

     # If we also have 1.6 (>> 1.6.1), use it instead because it is faster

      if automake-1.6 --version >/dev/null 2>&1; then
         if expr "`automake-1.6 --version | sed -e '1s/[^0-9]*//' -e q`" ">" "1.6.1" > /dev/null 2>&1; then
            amvers="-1.6"
         fi
      fi
   elif automake-1.7 --version >/dev/null 2>&1; then
      amvers="-1.7"

      # If we also have 1.6 (>> 1.6.1), use it instead because it is faster

      if automake-1.6 --version >/dev/null 2>&1; then
         if expr "`automake-1.6 --version | sed -e '1s/[^0-9]*//' -e q`" ">" "1.6.1" > /dev/null 2>&1; then
            amvers="-1.6"
         fi
      fi

   elif automake-1.6 --version >/dev/null 2>&1; then

      amvers="-1.6"
      if expr "`automake-1.6 --version | sed -e '1s/[^0-9]*//' -e q`" "<=" "1.6.1" > /dev/null 2>&1; then
         AUTOMAKE_BAD=yes
      fi

   elif automake-1.5 --version >/dev/null 2>&1; then

      INSTALL_BAD=yes
      amvers="-1.5"

   elif automake --version > /dev/null 2>&1; then

      amvers=""
      case "`automake --version | sed -e '1s/[^0-9]*//' -e q`" in
         0|0.*|1|1.[01234]|1.[01234][-.]*)
            amvers="none" ;;
         1.5|1.5.*)
            INSTALL_BAD=yes ;;
         1.6|1.6.0|1.6.1)
            AUTOMAKE_BAD=yes ;;
      esac
   fi

#******************************************************************************
# Check for the installation of the GNU gettext facility.
# Autopoint is available from 0.11.3, but we need at least 0.11.5
#------------------------------------------------------------------------------

# Check for pkg-config

   if pkg-config --version >/dev/null 2>&1; then
      PKGCONFIG=yes
   else
      PKGCONFIG=no
   fi

#************************************************************************
# Create config and m4 directories.  Note that they might be empty for
# this project.  Also create an include directory, mainly for "config.h"
# stuff.
#------------------------------------------------------------------------

   mkdir -p aux-files
   mkdir -p config
   mkdir -p m4
   mkdir -p po
   mkdir -p include

#************************************************************************
# Call a number of "auto" programs in the strict order shown below.  Note:
# Earlier versions of auto-tools don't ignore duplicate definitions of
# macros.  (The system normally provides m4 macros in /usr/share/aclocal,
# but the project also provides them in the project's m4 directory.)
#------------------------------------------------------------------------

# We still need to make aux-files/config.rpath and some other files
# available, since they are listed in configure.ac, and not provided by
# autoconf:

   cp contrib/config.rpath aux-files
   run_cmd ${ACLOCAL} -I ${M4DIR} --install
   run_cmd ${AUTOCONF}
   run_cmd ${AUTOHEADER}

#  The LT_INIT macro of libtool 2.0 (formerly called AC_PROG_LIBTOOL)
#  would do this, but Debian ships with version 1.5 libtool, so we have
#  to do things the old-fashioned way.

   run_cmd ${LIBTOOLIZE} --automake --force --copy
   run_cmd ${AUTOMAKE} --foreign --add-missing --copy

# Automake seems to need this one, but doesn't provide it!

   cp contrib/mkinstalldirs-1.10 aux-files/mkinstalldirs

# At this point, remove files which always need to be regenerated.
# Right now, this is done with the --clean option.

   case "$PKGCONFIG" in
     yes) ;;
     no) cat << E_O_F

   =============================================================================
   NOTE: The "pkg-config" utility is not installed on your system; detection of
      the gtk-2.0 and GNOME 2.0 libraries will not be reliable.
E_O_F
     ;;

   esac

   case "$AUTOMAKE_BAD" in
     no) ;;
     yes) cat << E_O_F

   =============================================================================
   NOTE: Your version of automake has a bug which prevents proper plugin
      compilation. Either compile Sequencer64 with the --disable-plugins flag, or
      use a version of automake newer than 1.6.1 (1.6.2 is OK, and so are
      the 1.5 series).
E_O_F
     ;;

   esac

   case "$INSTALL_BAD" in
     no) ;;
     yes) cat << E_O_F

   =============================================================================
   NOTE: Your version of automake has a bug which prevents proper installation.
      Do not use "make install" with this version of automake, or use a
      version of automake newer than 1.5 (such as 1.6 or 1.7).
E_O_F
     ;;

   esac

   if [ -x /usr/bin/dot ] ; then
      echo "Graphviz package found, can build diagrams in Doxygen."
   else
      echo "! To build the documentation, you need to install graphviz."
   fi

   if [ -x /usr/bin/doxygen ] ; then
      echo "Doxygen package found, can build the reference manual."
   else
      echo "! To build the documentation, you need to install doxygen."
   fi

   echo "Bootstrap complete at `date`" >> bootstrap.stamp

#************************************************************************
# --configure
#------------------------------------------------------------------------

   if [ "$DOBOOTSTRAP" == "yes" ] ; then

      echo "Bootstrapping enabled...."

      if [ "$DODEBUG" == "yes" ] ; then

         echo "Running './configure --enable-debug=gdb --disable-shared $EXTRAFLAGS'"
         ./configure --enable-debug=gdb --disable-shared $EXTRAFLAGS

      elif [ "$DORELEASE" == "yes" ] ; then

         echo "Running './configure --enable-silent-rules $EXTRAFLAGS'"
         ./configure --enable-silent-rules $EXTRAFLAGS
         echo "Note that running 'make V=1' will turn on full command output."

      else

         cat << E_O_F

Now run './configure' to configure Sequencer64 for compilation, or './configure
--help' for configuration options.  Useful options:

      --enable-debug=gdb  --disable-shared   --enable-silent-rules
      --disable-seq32jack --enable-alsamidi  --enable-rtmidi (default)
      --enable-cli (no GUI) --enable-portmidi --enable-qtmidi

E_O_F
      fi
   fi

fi

#******************************************************************************
# bootstrap (Sequencer64)
#------------------------------------------------------------------------------
# vim: ts=3 sw=3 wm=4 et ft=sh
#------------------------------------------------------------------------------
