#!/bin/sh

if ! test -e ../build/build_simphone; then
  echo
  echo ERROR: could not find ../build/build_simphone
  echo
  echo Please cd to the build directory where this script is and try again.
  echo
  exit 1
fi

PARAMS=$*
PARAM_NO_QT=0
PARAM_NO_SSL=0
PARAM_NO_DEPEND=1
CPUS=0
while test $# -gt 0; do
  case "$1" in
    --without-qt) PARAM_NO_QT=1;;
    --with-qt) PARAM_NO_QT=1;;
    --without-config-ssl) PARAM_NO_SSL=1;;
    --with-makedepend) PARAM_NO_DEPEND=0;;
    --with-install-exec) CPUS=1;;
    --without-install-exec) CPUS=0;;
  esac
  shift
done

if test $PARAM_NO_QT -eq 0; then
  if ! test -x ../qt/bin/qmake; then
    echo
    echo ERROR: could not find ../qt/bin/qmake
    echo
    echo Please make sure you have successfully ran ./build_qt to compile the Qt sources.
    echo
    exit 1
  fi
fi

cd ..
if ! test -e ./configure; then
  echo
  echo OOPS: configure script does not exist, trying to generate it...
  autoconf
  RET=$?
  if test $RET -ne 0; then
    echo
    if test $RET -ge 127; then
      echo Tough luck. You do not seem to have the 'autoconf' program installed.
      echo Either install it, or fetch the missing configure script and try again.
    else
      echo This trick seems to have failed. Fetch the configure script and try again.
    fi
    echo
    exit 1
  fi
fi

UNAME=`uname 2> /dev/null`
if test "x$UNAME" = "xFreeBSD"; then
  LIBDIR=`gcc -### 2>&1 | grep libdir | tr ' ' '\n' | grep libdir | cut -d = -f 2`
  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR
  export LD_LIBRARY_PATH
fi

echo
echo '********** ./configure' $PARAMS
echo
sh ./configure $PARAMS
RET=$?
if test $RET -ne 0; then
  echo
  echo ERROR $RET: could not configure simphone.
  echo
  exit 2
fi

# hack to try avoiding automake time travel
find speex speexdsp portaudio udev expat cares npth build/makedepend -name '*.m4' -exec touch -t200101010101 '{}' ';' 2>/dev/null
find speex speexdsp portaudio udev expat cares npth build/makedepend -name configure.ac -exec touch -t200101010101 '{}' ';' 2>/dev/null
find speex speexdsp portaudio udev expat cares npth build/makedepend -name Makefile.in -exec touch -t200101010101 '{}' ';' 2>/dev/null
find speex speexdsp portaudio udev expat cares npth build/makedepend -name Makefile.am -exec touch -t200101010101 '{}' ';' 2>/dev/null
# end of hack

if test $PARAM_NO_SSL -eq 0; then
  MAKEDEPEND=0
  if test $PARAM_NO_DEPEND -eq 0; then
    cd build/makedepend
    if test $? -eq 0; then
      make X_CFLAGS=-D_X_NORETURN= X_LIBS=
      MAKEDEPEND=$?
      if test $MAKEDEPEND -ne 0; then
        echo
        echo WARNING $MAKEDEPEND: could not compile makedepend
        echo
      fi
      cd ../..
      PATH=../../build/makedepend:../../../build/makedepend:$PATH
      export PATH
    fi
    make depend
    RET=$?
    if test $RET -ne 0; then
      echo
      echo ERROR $RET: could not configure openssl.
      echo
      if test $RET -ge 127; then
        echo Please make sure you have the 'make' program installed and try again.
        echo
      elif test $MAKEDEPEND -ne 0; then
        echo Please make sure you have the 'makedepend' program installed and then run:
        echo
        echo ./build_simphone --with-makedepend
        echo
      fi
      exit 3
    fi
  fi
fi

if test $CPUS -eq 0; then
  CPUS=`python -c "import multiprocessing;print(multiprocessing.cpu_count())" 2>/dev/null`
  if test -z "$CPUS"; then
    CPUS=1
  fi
fi
PARAMS="-j $CPUS"
if test -x /usr/local/bin/eg++; then
  if ! test -x /usr/local/bin/g++; then
    if ! test -x /usr/bin/g++; then
      PARAMS="$PARAMS CXX=eg++ CC=cc"
    fi
  fi
elif test -x /usr/bin/eg++; then
  if ! test -x /usr/bin/g++; then
    if ! test -x /usr/local/bin/g++; then
      PARAMS="$PARAMS CXX=eg++ CC=cc"
    fi
  fi
fi
echo
echo '********** gmake' $PARAMS
echo
gmake $PARAMS
RET=$?
if test $RET -ge 127; then
  gnumake $PARAMS
  RET=$?
  if test $RET -ge 127; then
    make $PARAMS
    RET=$?
  fi
fi
if test $RET -ne 0; then
  echo
  echo ERROR $RET: could not compile simphone.
  echo
  exit 4
fi
echo
echo Simphone compiled SUCCESSFULLY.
echo

cd qsimphone
if test $RET -eq 0; then
  if test "x$UNAME" = "xDarwin"; then
    cp qsimphone.app/Contents/MacOS/qsimphone simphone
    strip -no_uuid simphone
  else
    cp qsimphone simphone
    strip simphone
  fi
  chmod 0755 simphone 2>/dev/null
fi

exit 0
