#!/bin/sh

TARGET=oyranos
TARGET_NAME=Oyranos

ERROR=0
WARNING=0

ROOT_DIR="$0"
ROOT_DIR=`echo $ROOT_DIR | sed 's%/configure%%1'`
ROOT_DIR=`(cd $ROOT_DIR; pwd)`
BUILD_DIR="`pwd`"

zeile="$0 $@"

if [ `uname` = "Linux" ]; then
  v=-v
fi


time="`date +%y%m%d.%H%M%S`"
INFOECHO="echo"

CONF_LOG=config.log

X11=0
debug="0"

if [ $# -gt 0 ]; then
  prefix=""
  while [ $# -gt 0 ]; do
    arg=$1
    # prefix exec_prefix bindir xdgsysdir libdir includedir datadir mandir
    switch=prefix
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      prefix="`echo \"$1\" | sed s/\"--$switch=\"//`"
    fi
    switch="exec-prefix"
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      exec_prefix="`echo $1 | sed s/--$switch=//`"
    fi
    switch=bindir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      bindir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=xdgsysdir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      xdgsysdir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=libdir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      libdir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=cmmsubpath
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      cmmsubpath="`echo $1 | sed s/--$switch=//`"
    fi
    switch=includedir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      includedir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=datadir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      datadir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=mandir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      mandir="`echo $1 | sed s/--$switch=//`"
    fi
    # syscolordir usercolordir usercolordata iccdirname settingsdirname targetdirname pixmapdir icondir desktopdir
    switch=syscolordir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      syscolordir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=usercolordir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      usercolordir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=usercolordata
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      usercolordata="`echo $1 | sed s/--$switch=//`"
    fi
    switch=iccdirname
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      iccdirname="`echo $1 | sed s/--$switch=//`"
    fi
    switch=settingsdirname
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      settingsdirname="`echo $1 | sed s/--$switch=//`"
    fi
    switch=targetdirname
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      targetdirname="`echo $1 | sed s/--$switch=//`"
    fi
    switch=pixmapdir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      pixmapdir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=icondir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      icondir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=desktopdir
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      desktopdir="`echo $1 | sed s/--$switch=//`"
    fi
    switch=disable-fltk
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      fltk=0
    fi
    switch=with-fltk-config
    if [ "`echo \"$1\" | sed s/\"--$switch=\"//`" != "$1" ]; then
      fltkconfig="`echo $1 | sed s/--$switch=//`"
    fi
    switch=with-x
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      X11=1
    fi
    switch=with-rpath
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      rpath=1
    fi
    switch=rpm-only
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      rpm="1"
    fi
    switch=quick
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      quick="1"
    fi
    switch=enable-debug
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      debug="1"
    fi
    switch=enable-devel
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      devel="1"
    fi
    switch=disable-openmp
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      openmp="0"
    fi
    switch=disable-static
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      static="0"
    fi
    switch=enable-verbose
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      verbose="1"
    fi
    switch=disable-dependency-tracking
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      nodeps="1"
    fi
for i in $X_ADD_2; do
    checklib=$i switch=disable-lib$checklib
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      nochecklibs="$nochecklibs $checklib"
    fi
done
for i in $LIBS_TEST_1; do
    checklib=$i switch=disable-lib$checklib
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
      nochecklibs="$nochecklibs $checklib"
    fi
done
    switch=help
    if [ "`echo \"$1\" | sed s/\"--$switch\"//`" != "$1" ]; then
test -n "$INFOECHO" && $INFOECHO "Usage:"
test -n "$INFOECHO" && $INFOECHO "   --prefix=[/usr/local]                package prefix"
#test -n "$INFOECHO" && $INFOECHO "   --exec-prefix=[\$prefix]              package exec-prefix"
#test -n "$INFOECHO" && $INFOECHO "   --bindir=[\$exec-prefix/bin]          binary installation path"
test -n "$INFOECHO" && $INFOECHO "   --xdgsysdir=[/etc/xdg]               xdg config path"
test -n "$INFOECHO" && $INFOECHO "   --libdir=[\$exec-prefix/lib]          main library installation path"
#test -n "$INFOECHO" && $INFOECHO "   --cmmsubpath=[color/cmms]            module installation path"
#test -n "$INFOECHO" && $INFOECHO "   --includedir=[\$prefix/include]       include file installation"
#test -n "$INFOECHO" && $INFOECHO "                                        automatic appends oyranos/"
#test -n "$INFOECHO" && $INFOECHO "   --datadir=[\$prefix/data]             application data base path"
#test -n "$INFOECHO" && $INFOECHO "   --mandir=[\$datadir/man]              manual installation path"
#test -n "$INFOECHO" && $INFOECHO "   --syscolordir=[\$datadir/color]       Oyranos system color path"
#test -n "$INFOECHO" && $INFOECHO "   --usercolordir=[~/.config/color]     Oyranos user color config path"
#test -n "$INFOECHO" && $INFOECHO "   --usercolordata=[~/.local/share/color] Oyranos user color data path"
#test -n "$INFOECHO" && $INFOECHO "   --iccdirname=[icc]                   Oyranos ICC profile directory"
#test -n "$INFOECHO" && $INFOECHO "   --settingsdirname=[settings]         Oyranos settings directory"
#test -n "$INFOECHO" && $INFOECHO "   --targetdirname=[target]             Oyranos target directory"
#test -n "$INFOECHO" && $INFOECHO "   --pixmapdir=[\$datadir/pixmaps]       unthemed icon installation path"
#test -n "$INFOECHO" && $INFOECHO "   --icondir=[\$datadir/icons]           icon installation path"
#test -n "$INFOECHO" && $INFOECHO "   --desktopdir=[\$datadir/applications] xdg installation path"
#test -n "$INFOECHO" && $INFOECHO ""
#test -n "$INFOECHO" && $INFOECHO "   --with-x                             use X11 on Darwin"
#test -n "$INFOECHO" && $INFOECHO "   --enable-verbose                     verbose compiling [default enable]"
test -n "$INFOECHO" && $INFOECHO "   --help"
test -n "$INFOECHO" && $INFOECHO ""
      exit
    fi
    shift
  done
fi

# BSD sh shells do not support the -e option to echo
if [ "-e" = "`echo -e `" ]; then
  e=""
else
  e="-e"
fi

# config.log will not work as expected if called as pure BSD shell
test -z "$zeile" && zeile="exit 1" || echo ""

ECHO="echo"
export ECHO

$ECHO $e "$zeile \$@" > $CONF_LOG
$ECHO $e "exit 0" >> $CONF_LOG
$ECHO $e "" >> $CONF_LOG
chmod 0755 $CONF_LOG

if [ -n "$ECHO" ]; then
  echo_="";
  $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="$TARGET_NAME configuration helper"
  $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="                        $time                           "
  $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
fi


#prefix
if [ -z "$prefix" ]; then
  prefix=/usr/local
fi

# detect System

UNAME_=`uname`
OSUNAME=BSD
if [ -z "$MAKE" ]; then
  # One can override the make(1) executable to use; for instance on
  # platforms where GNU make is installed as gmake.
  MAKE=make
fi
if [ $? = 0 ] && [ $UNAME_ = "Darwin" ]; then
  arch=`uname -p`
  version=`uname -r`
  echo_="Darwin $arch $version   detected";
  test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
  APPLE="1"
  OSX_H="-DHAVE_OSX"
  OSUNAME=Darwin
else
  if [ $UNAME_ = "Linux" ]; then
    echo_="Linux system            detected"
    test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
    SYS=0
    if [ -f /etc/SuSE-release ]; then
      if [ `cat /etc/SuSE-release | grep Linux | wc -l` -ne 0 ]; then
        echo_="`cat /etc/SuSE-release | grep Linux`"
        test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
        SYS=1; fi
      if [ $SYS -eq 0 ]; then
        echo_="`cat /etc/SuSE-release | grep SUSE`"
        test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
        SYS=1; fi fi
    if [ -f /etc/redhat-release ]; then
      echo_="`cat /etc/redhat-release`"
      test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
      SYS=1; fi
    if [ -f /etc/debian_version ]; then
      if [ -f /etc/issue.net ]; then
        echo_="`cat /etc/issue.net || cat /etc/debian_version`"
        test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
        SYS=2; fi; fi
    if [ $SYS -eq 0 ]; then
      if [ -f /etc/issue.net ]; then
        echo_="`cat /etc/issue.net`"
        test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
    fi; fi
    OSUNAME=Linux
  else
    if [ $UNAME_ = "SunOS" ]; then
      echo_="SunOS                   detected"
      test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
      OSUNAME=SunOS
    else
      if [ $UNAME_ = "OpenBSD" ] || [ $UNAME_ = "NetBSD" ] || [ $UNAME_ = "FreeBSD" ]; then
        if [ "$MAKE" = 'make' ]; then
          OS_EXT=.bsd
        fi
        echo_="BSD $UNAME_             detected"
        test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
        OSUNAME=BSD
      else
        if [ `uname -a | grep W32 | wc -l` -gt 0 ]; then
          echo_="Windows $UNAME_             detected"
          test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
          OSUNAME=WIN32
        else
          echo_="Oyranos may or may not compile on your $UNAME_ system"
          test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
        fi
      fi
    fi
  fi
fi
OS_EXT=""

# detect the compilers bus width options
BARCH=
rm -f intptr_test
if [ -z "$CC" ]; then
  COMPILER=cc
else
  COMPILER=$CC
fi
$COMPILER $CFLAGS $ROOT_DIR/src/tests/intptr_test.c -o intptr_test 2>/dev/null
FPIC=-fPIC
DPIC=-DPIC
if [ -f intptr_test ]; then
  ./intptr_test
  INTPTR_SIZE=$?
  if [ $INTPTR_SIZE -eq 4 ]; then
    echo_="32-bit host"
  elif [ $INTPTR_SIZE -gt 4 ]; then
    echo_="64-bit host"
    if [ "$OSUNAME" != "BSD" ]; then
      BARCH=64
    fi
  elif [ $INTPTR_SIZE -ne 0 ]; then
    echo_="$INTPTR_SIZE-byte intptr_t          detected"
  else
    echo_="CPU bus width not         detected"
  fi
  test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
  rm intptr_test
else
  $COMPILER $CFLAGS $ROOT_DIR/src/tests/intptr_test.c -o intptr_test
fi

MACH_=`uname -m`
deb_arch=$MACH_
if [ $? = 0 ]; then
  if [ "$MACH_" = "x86_64" ]; then
    echo_="X86_64                  detected"
    deb_arch=amd64
    test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
  else
    if [ "$MACH_" = "sun4u" ]; then
      echo_="spark ultra 4           detected"
      test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
    else
      echo_="$MACH_ system             detected"
      test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
      if [ "$MACH_" = "i686" ]; then
        deb_arch=i386
      fi
    fi
  fi
fi
LIBDIR="lib"$BARCH
export OSUNAME 
echo_="deb_arch=$deb_arch"
test -n "$ECHO" && test -n "$INFOECHO" && $INFOECHO "$echo_";


# exec_prefix bindir xdgsysdir libdir cmmsubpath includedir datadir
if [ -z "$exec_prefix" ]; then
  exec_prefix="$prefix"
fi
if [ -z "$bindir" ]; then
  bindir="$exec_prefix/bin"
fi
if [ -z "$xdgsysdir" ]; then
  xdgsysdir="/etc/xdg"
fi
if [ -z "$libdir" ]; then
  libdir="$exec_prefix/$LIBDIR"
else
  if [ `echo $libdir | grep 64\$ | wc -l` -eq 1 ]; then
    BARCH=64
  else
    BARCH=""
  fi
  LIBDIR=lib$BARCH
  libdir="$exec_prefix/$LIBDIR"
fi
metasubpath=$TARGET
metadir="$libdir/$metasubpath"
if [ -z "$cmmsubpath" ]; then
  cmmsubpath="colour/modules"
fi
cmmdir="$libdir/$cmmsubpath"
if [ -z "$includedir" ]; then
  includedir="$prefix/include"
fi
if [ -z "$datadir" ]; then
  datadir="$prefix/share"
fi
if [ -z "$mandir" ]; then
  mandir="$datadir/man"
fi
# syscolordir usercolordir usercolordata iccdirname settingsdirname targetdirname icondir pixmapdir desktopdir
if [ -z "$syscolordir" ]; then
  syscolordir="$datadir/color"
fi
if [ -z "$usercolordir" ]; then
  usercolordir="~/.config/color"
fi
if [ -z "$usercolordata" ]; then
  usercolordata="~/.local/share/color"
fi
if [ -z "$iccdirname" ]; then
  iccdirname="icc"
fi
if [ -z "$settingsdirname" ]; then
  settingsdirname="settings"
fi
if [ -z "$targetdirname" ]; then
  targetdirname="target"
fi
if [ -z "$pixmapdir" ]; then
  pixmapdir="$datadir/pixmaps"
fi
if [ -z "$icondir" ]; then
  icondir="$datadir/icons"
fi
if [ -z "$desktopdir" ]; then
  desktopdir="$datadir/applications"
fi

PATH=$bindir:$PATH
export PATH

if [ -n "$CC" ]; then
  CC=cc
fi
if [ -n "$CXX" ]; then
  CXX=c++
fi

export CC CXX


# export
for i in CFLAGS CXXFLAGS LDFLAGS; do
  if [ -n "$i" ]; then
    export $i
    #test -n "$INFOECHO" && $INFOECHO export $i
  else
    unset $i
  fi
done
if [ -n "$ECHO" ]; then
  echo_="\"$CFLAGS\" \"$CXXFLAGS\" \"$LDFLAGS\""
  test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
  test -n "$ECHO" && $ECHO $e "$echo_" >> $CONF_LOG; test -n "$INFOECHO" && $INFOECHO "$echo_"
fi

export CFLAGS CXXFLAGS LDFLAGS

if [ -n "$ECHO" ]; then
  echo_=""
  echo_="#                       Paths                                  #"
  echo_="#                                                              #"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="prefix          =       $prefix"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="exec_prefix     =       $exec_prefix"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="bindir          =       $bindir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="xdgsysdir       =       $xdgsysdir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="libdir          =       $libdir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="cmmdir          =       $cmmdir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="cmmsubpath      =       $cmmsubpath"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="includedir      =       $includedir (install: += /oyranos)"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="datadir         =       $datadir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="mandir          =       $mandir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="syscolordir     =       $syscolordir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="usercolordir    =       $usercolordir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="usercolordata   =       $usercolordata"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="iccdirname      =       $iccdirname"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="settingsdirname =       $settingsdirname"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="targetdirname =         $targetdirname"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="pixmapdir       =       $pixmapdir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="icondir         =       $icondir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="desktopdir      =       $desktopdir"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_="################################################################"
  test -n "$INFOECHO" && $INFOECHO "$echo_"
  echo_=""
  test -n "$INFOECHO" && $INFOECHO "$echo_"
fi

if [ "$debug" = "1" ]; then
  export CFLAGS="-Wall -g -DDEBUG=1 $CFLAGS"
  debug_flags=CFLAGS="\"$CFLAGS\""
fi

echo "$debug_flags cmake -DCMAKE_INSTALL_PREFIX=$prefix -DXDG_CONFIG_DIR=$xdgsysdir -DLIB_SUFFIX=$BARCH $ROOT_DIR"
cmake -DCMAKE_INSTALL_PREFIX=$prefix -DXDG_CONFIG_DIR=$xdgsysdir -DLIB_SUFFIX=$BARCH $ROOT_DIR

exit 0
