#!/bin/bash

# configure script for
#
#  dchat/egalite
#
# 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 determine the name of qmake
#
findqmake () {

  QM=`which qmake`
  QM4=`which qmake-qt4`

  if [ -z $QM ] 
  then
    RES3="0"
  else
    RES3=`$QM -query QT_VERSION`
  fi
  if [ -z $QM4 ]
  then
    RES4="0"
  else
    RES4=`$QM4 -query QT_VERSION`
  fi

  MATCH3=`expr match $RES3 '4.'`
  MATCH4=`expr match $RES4 '4.'`

  QMAKE="qmake"

  if [ $MATCH3 -eq 2 ]
  then
    QMAKE=$QM
  else
    if [ $MATCH4 -eq 2 ]
    then
      QMAKE=$QM4
    else
      echo "cannot find qmake for Qt4"
      exit 1
    fi
  fi
}


#
# 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
}

find_lrelease () {

  LREL=`which lrelease`
  LREL4=`which lrelease-qt4`

  if [ -z $LREL ] 
  then
    RES3="0"
  else
    RES3=`$LREL -version`
  fi
  if [ -z $LREL4 ]
  then
    RES4="0"
  else
    RES4=`$LREL4 -version`
  fi

  LRMATCH3=`expr match "$RES3" 'lrelease version 4.'`
  LRMATCH4=`expr match "$RES4" 'lrelease version 4.'`

  LRELEASE="lrelease"

  if [ $LRMATCH3 -eq 19 ]
  then
    LRELEASE=$LREL
  else
    if [ $LRMATCH4 -eq 19 ]
    then
      LRELEASE=$LREL4
    else
      echo "cannot find LRELEASE for Qt4"
      exit 1
    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 "--qxmppbase=QXDIR  qxmpp base directory [${QXMPP_BASE}]"
   echo "--qxmppname=QXNAME qxmpp basic name [${QXMPP_NAME}]"
   echo "--audio=<0|1>      Build with audio input support [${DO_AUDIO}]"
   echo "--mobil-audio=<0|1>  Build with qtmobile audio [${DO_MOBI_AUDIO}]"
   echo
}


#
# main configure script
#

COPYRIGHT=" Copyright (C) 2011 Bernd Stramm"
PREFIX=/usr
QXMPP_BASE=/usr
QXMPP_NAME=qxmppclient
OPTIONS_FILE=options.pri
DO_AUDIO=0
DO_MOBI_AUDIO=1
COMMAND_LINE="$0 $*"
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
   ;;
  --qxmppbase)
   QXMPP_BASE=$ac_optarg
   $ac_shift
   ;;
  --qxmppname)
   QXMPP_NAME=$ac_optarg
   $ac_shift
   ;;
  --audio)
   DO_AUDIO=$ac_optarg
   $ac_shift
   ;;
  --mobil-audio)
   DO_MOBI_AUDIO=$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"
  LRELEASE="lrelease.exe"
else
  MAKE_PROGRAM="make"
  # 
  # check what qmake is called on this system
  #
  findqmake
  find_lrelease
fi

#
#  see if we build for x86 or ARM
#
MACHINE=`uname -m`
IS_X86=`expr index 86 $MACHINE`
IS_ARM=`expr index arm $MACHINE`

#
# 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=\${PREFIX}/bin" >> Makefile
echo "QMAKE = $QMAKE" >> Makefile
echo "MAKE = ${MAKE_PROGRAM}" >> Makefile
echo "LRELEASE = ${LRELEASE}" >> Makefile

#
# make options include file for qmake
#
echo "DISTRO = ${DISTRO}" > ${OPTIONS_FILE}
echo "CONFIG += ${DISTRO}" >> ${OPTIONS_FILE}
echo "DEFINES += DISTRO_${DISTRO}=1" >> ${OPTIONS_FILE}
echo "QXMPP_BASE = ${QXMPP_BASE}" >> ${OPTIONS_FILE}
echo "QXMPP_NAME = ${QXMPP_NAME}" >> ${OPTIONS_FILE}
echo "DO_AUDIO = ${DO_AUDIO}" >> ${OPTIONS_FILE}
echo "DO_MOBI_AUDIO = ${DO_MOBI_AUDIO}" >> ${OPTIONS_FILE}
if [ $DO_AUDIO -eq 1 ]
then
  echo "CONFIG += audio" >> ${OPTIONS_FILE}
else 
  echo "CONFIG -= audio" >> ${OPTIONS_FILE}
fi
if [ $DO_MOBI_AUDIO -eq 1 ]
then
  echo "CONFIG += mobilaudio" >> ${OPTIONS_FILE}
else 
  echo "CONFIG -= mobilaudio" >> ${OPTIONS_FILE}
fi
if [ $IS_ARM -eq 1 ]
then
  echo "CONFIG += build_arm" >> ${OPTIONS_FILE}
else
  echo "CONFIG -= build_arm" >> ${OPTIONS_FILE}
fi
if [ $IS_X86 -eq 1 ]
then
  echo "CONFIG += build_x86" >> ${OPTIONS_FILE}
else
  echo "CONFIG -= build_x86" >> ${OPTIONS_FILE}
fi
#
# 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 'qmake is..................' $QMAKE
showmsg 'Installation prefix is....' $PREFIX 
showmsg 'Distro setting is.........' $DISTRO
showmsg 'DO_AUDIO is...............' $DO_AUDIO
showmsg 'DO_MOBI_AUDIO is..........' $DO_MOBI_AUDIO
showmsg 'Qxmpp base dir is.........' $QXMPP_BASE
showmsg 'Qxmpp name is.............' $QXMPP_NAME
showmsg 'IS_ARM is.................' $IS_ARM
showmsg 'IS_X86 is.................' $IS_X86
showmsg 'Now you can build with....' $MAKE_PROGRAM 

