#!/bin/bash

# configure script for
#
#  msim
#
# Copyright (C) 2011 Bernd Stramm
#

#/****************************************************************
# * This file is distributed under the following license:
# *
# * Copyright (C) 2011, Bernd Stramm
# *
# *  This program is free software; you can redistribute it and/or
# *  modify it under the terms of the GNU General Public License
# *  as published by the Free Software Foundation; either version 2
# *  of the License, or (at your option) any later version.
# *
# *  This program is distributed in the hope that it will be useful,
# *  but WITHOUT ANY WARRANTY; without even the implied warranty of
# *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# *  GNU General Public License for more details.
# *
# *  You should have received a copy of the GNU General Public License
# *  along with this program; if not, write to the Free Software
# *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
# *  Boston, MA  02110-1301, USA.
# ****************************************************************/
#


#
# function to find distro
#
find_distro () {
DISTRO=linux
if [ -e /etc/system-release ]
then
  DISTRO=`head -1 /etc/system-release \
         | awk '{print $1}' \
         | tr '[:upper:]' '[:lower:]'`
else
  if [ -e /etc/issue ]
  then
    DISTRO=`head -1 /etc/issue \
	| awk '{print $1}' \
	| tr '[:upper:]' '[:lower:]'`
  fi
fi
}

#
# function to tell the user about options
#
usage () {
   echo 
   echo "$0 [OPTIONS]"
   echo
   echo "Options:"
   echo "--help             Show this help message and quit"
   echo "--prefix=PREFIX    Install into PREFIX [${PREFIX}]"
   echo "--libdir=LIBDIR    Destination lib directory [${LIBDIR}]"
   echo "--distro=DISTRO    Build assuming DISTRO [${DISTRO}]"
   echo
}


#
# main configure script
#

COPYRIGHT=" Copyright (C) 2011 Bernd Stramm"
PREFIX=/usr
#PREFIX=${HOME}
LIBDIR=/usr/lib
COMMAND_LINE="$0 $*"
OPTIONS_FILE=make.inc

find_distro

# 
# command line arguments:
# see if they said anything about prefix
#
while test $# != 0
do
  case $1 in
  --*=*)
  ac_option=`expr "X$1" : 'X\([^=]*\)='`
  ac_optarg=`expr "X$1" : 'X[^=]*=\(.*\)'`
  ac_shift=:
  ;;
 *)
  ac_option=$1
  ac_optarg=$2
  ac_shift=shift
  ;;
  esac

case $ac_option in
  --help)
   usage
   exit 0
   ;;
  --prefix)
   PREFIX=$ac_optarg
   $ac_shift
   ;;
  --libdir)
   LIBDIR=$ac_optarg
   $ac_shift
   ;;
  --distro)
   DISTRO=$ac_optarg
   $ac_shift
   ;;
  *)
   echo "Bad option " $ac_option " " $ac_optarg
   usage
   shift
   ;;
  esac
shift

done 

#
# see if we are on windows and need a special name for "make"
#

OSLEN=`expr match "Z$OS" "ZWindow"`
if [ $OSLEN -gt 5 ] ; then
  MAKE_PROGRAM="mingw32-make.exe"
  QMAKE="qmake.exe"
else
  MAKE_PROGRAM="make"
fi

#
#  see if we build for x86 or ARM
#
MACHINE=`uname -m`

#
# make options include file for make
#
echo "DISTRO = ${DISTRO}" > ${OPTIONS_FILE}
echo "CONFIG += ${DISTRO}" >> ${OPTIONS_FILE}
echo "DEFINES += DISTRO_${DISTRO}=1" >> ${OPTIONS_FILE}
#
# generate Makefile
#
# first shove in everything we found out
#
echo "#" > Makefile
echo "# Makefile generated by configure" >> Makefile
echo "# Faux automake" >> Makefile
echo "# ${COPYRIGHT}" >> Makefile
echo "#" >> Makefile
echo "PREFIX=${PREFIX}" >> Makefile
echo "DESTDIR=" >> Makefile
echo "LIBDIR=${LIBDIR}" >> Makefile
echo "MAKE = ${MAKE_PROGRAM}" >> Makefile

#
# next concatenate the Makefile.in content
#
cat Makefile.in >> Makefile

#
# tell the user what to do next
#
showmsg () {
  echo "     " $* 
  echo $* >> config.last
}

echo "" > config.last
showmsg `date -R`
showmsg $COMMAND_LINE
showmsg ""
showmsg 'Installation prefix is....' $PREFIX 
showmsg 'Lib destination dir is....' $LIBDIR 
showmsg 'Distro setting is.........' $DISTRO
showmsg 'Make program is...........' $MAKE_PROGRAM 

