#!/bin/sh

if [ "$1" = "-h" -o "$1" = "--help" -o "$1" = "-help" ]; then
    echo "Usage: $0 [OPTION]... [VAR=VALUE]..."
    echo ""
    echo "Installation directories:"
    echo "  --prefix=PREFIX       : install in PREFIX (e.g. /usr) [default: /usr/local]"
    echo ""
    echo "Optional Packages:"
    echo "  --with-gtk=2.0|3.0    : which gtk+ version to compile against [default: 2.0]"
    echo ""
    echo "Optional Features:"
    echo "  --disable-nls            : disable Native Language Support [default: enable]"
    echo "  --disable-xim            : disable the support of XIM [default: enable]"
    echo "  --disable-system-tray    : disable system tray support [default: enable]"
    echo "  --disable-tsin           : disable Tsin input method [default: enable]"
    echo "  --disable-gtk2-im-module : disable GTK+ 2.x im-module [default: auto-detect]"
    echo "  --disable-gtk3-im-module : disable GTK+ 3.x im-module [default: auto-detect]"
    echo "  --enable-qt3-immodule    : enable QT3 im-module [default: disable]"
    echo "  --disable-qt4-immodule   : disable QT4 im-module [default: auto-detect]"
    echo "  --disable-anthy          : disable Anthy input method [default: auto-detect]"
    echo "  --disable-chewing        : disable Chewing input method [default: auto-detect]"
    echo "  --disable-appindicator   : disable Ubuntu unity tray [default: auto-detect]"
    echo ""
    echo "System Specific Pathes:"
    echo "  --qt3-moc-path=PATH       : specify the path of \"moc\" command for Qt 3.x"
    echo "  --qt4-moc-path=PATH       : specify the path of \"moc\" command for Qt 4.x"
    echo "  --qt4-im-module-path=PATH : specify the install path for Qt 4.x im-module."
    echo ""
    exit
fi


MAKE=make
which gmake > /dev/null 2> /dev/null
if [ $? = 0 ]; then
  MAKE=gmake
fi

FREEBSD=0
uname | grep FreeBSD > /dev/null 2> /dev/null
if [ $? = 0 ]; then
  FREEBSD=1
fi

uname | grep OpenBSD > /dev/null 2> /dev/null
if [ $? = 0 ]; then
  FREEBSD=1
fi

SO_FLAGS="$LDFLAGS -shared -Wl,--as-needed"
MAC_OS=0
uname | grep Darwin > /dev/null 2> /dev/null
if [ $? = 0 ]; then
  FREEBSD=1
  MAC_OS=1
  SO_FLAGS="-dynamic -bundle -undefined suppress -flat_namespace"
fi

prefix="/usr/local"
use_xim='Y'
use_tray='Y'
use_unity='Y'
use_i18n='Y'
use_tsin='Y'
use_anthy='Y'
use_chewing='Y'
# Qt3 immodule patch is not included in official Qt3 release.
# Some distros do not apply that patch and will always build failure.
# We should turn off Qt3 immodule by default.
use_qt3='N'
use_qt4='Y'
use_gtk='Y'
use_gtk3='Y'

find_in_path_list_to_variable() {
    # find_in_path_list_to_variable() will find all elements in the array name stored in $1
    # if found, set the found path to variable name in $2 and return 0
    # otherwise return 1.
	 
	 for path in $1; do
		  if [ -x "$path" ]; then
				eval $2=\"$path\"
				return 0
		  fi
	 done
	 return 1
}

#                  debian                 fedora               fedora           freebsd                slackware          arch
qt4_moc_path_list="/usr/share/qt4/bin/moc /usr/lib/qt4/bin/moc /usr/bin/moc-qt4 /usr/local/bin/moc-qt4 /usr/local/bin/moc /usr/bin/moc"

#                  debian                 others
qt3_moc_path_list="/usr/share/qt3/bin/moc /opt/qt/bin/moc"

if [ "$HIME_USE_XIM" ]; then
  use_xim=$HIME_USE_XIM
fi

QT4_IM_DIR_CUSTOM=
for opt do
  case "$opt" in
  --with_gtk3|--with-gtk=3.0) GTK=gtk+-3.0
  ;;
  --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --use_xim=*) use_xim=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-xim) use_xim=Y
  ;;
  --disable-xim) use_xim=N
  ;;
  --use_tray=*) use_tray=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-system-tray) use_tray=Y
  ;;
  --disable-system-tray) use_tray=N
  ;;
  --use_unity=*) use_unity=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-appindicator) use_unity=Y
  ;;
  --disable-appindicator) use_unity=N
  ;;
  --use_i18n=*) use_i18n=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-nls) use_i18n=Y
  ;;
  --disable-nls) use_i18n=N
  ;;
  --use_tsin=*) use_tsin=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-tsin) use_tsin=Y
  ;;
  --disable-tsin) use_tsin=N
  ;;
  --use_anthy=*) use_anthy=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-anthy) use_anthy=Y
  ;;
  --disable-anthy) use_anthy=N
  ;;
  --use_chewing=*) use_chewing=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-chewing) use_chewing=Y
  ;;
  --disable-chewing) use_chewing=N
  ;;
  --use_qt3=*) use_qt3=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-qt3-immodule) use_qt3=Y
  ;;
  --disable-qt3-immodule) use_qt3=N
  ;;
  --use_qt4=*) use_qt4=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-qt4-immodule) use_qt4=Y
  ;;
  --disable-qt4-immodule) use_qt4=N
  ;;
  --use_gtk=*) use_gtk=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-gtk2-im-module) use_gtk=Y
  ;;
  --disable-gtk2-im-module) use_gtk=N
  ;;
  --use_gtk3=*) use_gtk3=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-gtk3-im-module) use_gtk3=Y
  ;;
  --disable-gtk3-im-module) use_gtk3=N
  ;;
  --qt3-moc-path=*) QT3_MOC_PATH=`echo $opt | cut -d '=' -f 2`
  ;;
  --qt4-moc-path=*) QT4_MOC_PATH=`echo $opt | cut -d '=' -f 2`
  ;;
  --qt4-install-to=*|--qt4-im-module-path=*) QT4_IM_DIR_CUSTOM=`echo $opt | cut -d '=' -f 2`
  ;;
  esac
done

echo "prefix: $prefix"

echo -n ".... Testing Xtst               :  "
XTST=`pkg-config --exists xtst 2> /dev/null`
if [ $? != 0 ]; then
  echo "please install rpm/package xtest-devel. abort!"
  exit 1
else
  echo "Found."
fi

if [ -z "$GTK" ]; then
  GTK=gtk+-2.0
fi

echo -n ".... Testing $GTK           :  "

GTKINC=`pkg-config --cflags $GTK 2> /dev/null`

if [ $? != 0 ]; then
  if [ "$GTK" = gtk+-2.0 ]; then
    echo "Not found, trying gtk+-3.0..."
    GTK=gtk+-3.0
    echo -n ".... Testing $GTK           :  "
  
    GTKINC=`pkg-config --cflags $GTK 2> /dev/null`
    if [ $? != 0 ]; then
      echo "$GTK or above required --cflags. abort!"
      rm -f config.mak
      exit 1
    fi
  else
    echo "$GTK or above required --cflags. abort!"
    rm -f config.mak
    exit 1
  fi
fi

## problem in MacOS, by candyz
if [ $MAC_OS = 1 ]; then
  GTKINC="-I/sw/include `pkg-config --cflags $GTK` 2> /dev/null"
fi

# TODO: Do we really need these craps?
if [ ! -x /bin/bash ]; then
  for i in /usr/bin/bash /usr/local/bin/bash
  do
    [ -x $i ] || continue
    echo "/bin/bash -> $i"
    for i in scripts/*
    do
      sed -e "s~/bin/bash~$i~" < $i > $i.tmp
      mv $i.tmp $i
    done
  done
fi


GTKLDFLAGS=`pkg-config --libs $GTK 2> /dev/null`

if [ $? != 0 ]; then
  echo "$GTK or above required --libs"
  echo "please install rpm/package libgtk+2.0_0-devel/libgtk+3.0_0-devel. abort!"
  rm -f config.mak
  exit 1
else
  echo "Found."
fi

if [ "$GTK" = gtk+-2.0 ]; then
  pkg-config --exists 'gtk+-2.0 >= 2.10' 2> /dev/null
  if [ $? != 0 ]; then
    echo ".... Testing system-tray     :  Disabled. gtk > 2.10 is needed to enable system-tray."
    use_tray=N
  fi
fi

echo -n ".... Testing anthy              :  "
USE_ANTHY=`pkg-config --libs anthy 2> /dev/null`
if [ $? != 0 ]; then
  echo "Not found, anthy module is turned off."
  USE_ANTHY=N
else
  if [ "$use_anthy" = "N" ]; then
    USE_ANTHY='N'
    echo "Found, but disabled."
  else
    USE_ANTHY='Y'
    echo "Found."
  fi
fi

echo -n ".... Testing chewing            :  "
USE_CHEWING=`pkg-config --libs chewing 2> /dev/null`
if [ $? != 0 ]; then
  echo Not found, chewing module is turned off.
  USE_CHEWING=N
else
  if [ $use_chewing = "N" ]; then
    USE_CHEWING='N'
    echo "Found, but disabled."
  else
    pkg-config --exists 'chewing >= 0.3.2' 2> /dev/null
    if [ $? != 0 ]; then
      USE_CHEWING='N'
      echo "Disabled. chewing > 0.3.2 is needed to enable chewing."
    else
      USE_CHEWING='Y'
      echo "Found."
    fi
  fi
fi

if [ "$GTK" = "gtk+-3.0" ]; then
  APPINDICATOR=appindicator3-0.1
else
  APPINDICATOR=appindicator-0.1
fi
if [ "$use_unity" = "Y" ]; then
  echo -n ".... Testing appindicator       :  "
  USE_UNITY=`pkg-config --libs $APPINDICATOR 2> /dev/null`
  if [ $? != 0 ]; then
    echo "Not found, unity tray is turned off."
    USE_UNITY=N
  else
    if [ "$use_unity" = "N" ]; then
      USE_UNITY='N'
      echo "Found, but disabled."
    else
      pkg-config --exists "$APPINDICATOR >= 0.4.0" 2> /dev/null
      if [ $? != 0 ]; then
        echo "Disabled. appindicator > 0.4.0 is needed to enable appindicator."
        USE_UNITY='N'
      else
        USE_UNITY='Y'
        echo "Found."
        UNITY_INC="-I/usr/include/libappindicator-0.1"
        GTKINC=`pkg-config --cflags $GTK $APPINDICATOR 2> /dev/null`
        GTKLDFLAGS=`pkg-config --libs $GTK $APPINDICATOR 2> /dev/null`
      fi
    fi
  fi
fi

LIB='lib'
if [ -d /usr/lib64 -a -d /lib64 ]; then
LIB='lib64'
fi
DEBIAN_LD=`ld -v 2>&1 | grep -i debian`
UBUNTU_LD=`ld -v 2>&1 | grep -i ubuntu`
if [ "$DEBIAN_LD" != "" -o "$UBUNTU_LD" != "" ]; then
  LIB='lib'
fi

# --- QT3 START ---
QT_IM='N'
QT_IM_DIR=/qt3/plugins/inputmethods
echo -n ".... Testing Qt 3.x             :  "
pkg-config --libs qt-mt > /dev/null 2> /dev/null
if [ $? = 0 ]; then
  QT_IM='Y'
fi

if [ ! -x "$QT3_MOC_PATH" ]; then
	 find_in_path_list_to_variable "$qt3_moc_path_list" "QT3_MOC_PATH"
fi

if [ "$QT_IM" = "Y" ]; then
    if [ "$use_qt3" = "N" ]; then
        QT_IM='N'
        echo "Found, but disabled."
    else
		  if [ ! -x "$QT3_MOC_PATH" ]; then
				echo "Not found, cannot find \"moc\" tool for Qt 3.x."
				echo "Hint: use the --qt3-moc-path option. abort!"
				exit 1
		  fi
        echo "Found."
    fi
else
    echo "Not found, Qt 3.x immodule is turned off."
fi
# --- QT3 END ---

# --- QT4 START ---
QT4_IM='N'
if [ -z "$QT4_IM_DIR" ]; then QT4_IM_DIR=/qt4/plugins/inputmethods; fi

echo -n ".... Testing Qt 4.x             :  "
pkg-config --libs QtCore QtGui > /dev/null 2> /dev/null
if [ $? = 0 ]; then
  QT4_IM='Y'
fi

if [ ! -x "${QT4_MOC_PATH}" ]; then
	 find_in_path_list_to_variable "$qt4_moc_path_list" "QT4_MOC_PATH"
fi

if [ "$QT4_IM" = "Y" ]; then
    if [ "$use_qt4" = "N" ]; then
        QT4_IM='N'
        echo "Found, but disabled."
    else
		  if [ ! -x "$QT4_MOC_PATH" ]; then
				echo "Not found, cannot find \"moc\" tool for Qt 4.x."
				echo "Hint: use the --qt4-moc-path option. abort!"
				exit 1
		  fi		  
        echo "Found."
    fi
else
    echo "Not found, Qt 4.x immodule is turned off."
fi
# --- QT4 END ---

# --- MOC INFO START ---

if [ "$QT_IM" = "Y" ]; then echo -e ".... Path of Qt3 moc            :  ${QT3_MOC_PATH}"; fi
if [ "$QT4_IM" = "Y" ]; then echo -e ".... Path of Qt4 moc            :  ${QT4_MOC_PATH}"; fi

# --- MOC INFO END ---

if [ "$GTK"=gtk+-3.0 ]; then
  GTK_IM='N'
  echo -n ".... Testing GTK+ 2.x immodule  :  "
  pkg-config --libs gtk+-2.0 > /dev/null 2> /dev/null
  if [ $? = 0 ]; then
    GTK_IM='Y'
  fi
  
  if [ "$GTK_IM" = "Y" ]; then
      if [ "$use_gtk" = "N" ]; then
          GTK_IM='N'
          echo "Found, but disabled."
      else
          echo "Found."
      fi
  else
      echo "Not found. It's ok if you don't want GTK+ 2.x immodule."
  fi
fi

GTK3_IM='N'
echo -n ".... Testing GTK+ 3.x immodule  :  "
pkg-config --libs gtk+-3.0 > /dev/null 2> /dev/null
if [ $? = 0 ]; then
  GTK3_IM='Y'
fi

if [ "$GTK3_IM" = "Y" ]; then
    if [ "$use_gtk3" = "N" ]; then
        GTK3_IM='N'
        echo "Found, but disabled."
    else
        echo "Found."
    fi
else
    echo "Not found. It's ok if you don't want GTK+ 3.x immodule."
fi

[ -f "data/dayi3.cin" ] && INSTALL_DFSG_INCOMPATIBLE=Y

if [ -d .git ]; then
  GIT_HAVE='Y'
  GIT_HASH=`git log 2> /dev/null | head -n 1 | cut -d ' ' -f 2 | head -c 7`
fi

bindir=$prefix/bin
datadir=$prefix/share
if [ $FREEBSD -eq 0 ]; then
  mandir=$datadir/man
else
  mandir=$prefix/man
fi
libdir=$prefix/$LIB
includedir=$prefix/include
optdir=/opt

BUILD_MODULE='N'
if [ "$USE_ANTHY" = 'Y' ] || [ "$USE_CHEWING" = 'Y' ]; then
  BUILD_MODULE='Y'
fi

if [ -z "$CFLAGS" ]; then
  OPTFLAGS="-Wall"
else
  OPTFLAGS="$CFLAGS"
fi
echo "-> CFLAGS = $CFLAGS."

if [ -z "$CC" ]; then
  CC=gcc
fi

if [ -z "$CXX" ]; then
  CXX=g++
fi

echo "CC=$CC" > config.mak
echo "CCX=$CC" >> config.mak
echo "CXX=$CXX" >> config.mak
echo "CCLD=$CC" >> config.mak
echo "MAKE=$MAKE" >> config.mak
echo "GTK=$GTK" >> config.mak
echo "GTKINC=$GTKINC" >> config.mak
echo "prefix=$prefix" >> config.mak
echo "bindir=\$(DESTDIR)$bindir" >> config.mak
echo "mandir=\$(DESTDIR)$mandir" >> config.mak
echo "man1dir=\$(mandir)/man1" >> config.mak
echo "bindir_r=\$(DESTDIR)$bindir" >> config.mak
echo "datadir=\$(DESTDIR)$datadir" >> config.mak
echo "datadir_r=\$(DESTDIR)$datadir" >> config.mak
echo "filterdir=\$(datadir_r)/hime/filter" >> config.mak
echo "libdir=\$(DESTDIR)$libdir" >> config.mak
echo "himelibdir=\$(libdir)/hime" >> config.mak
echo "includedir=\$(DESTDIR)$includedir" >> config.mak
echo "LDFLAGS_ENV=$LDFLAGS" >> config.mak
echo "LDFLAGS=$LDFLAGS -Wl,--as-needed -lX11 -lm" >> config.mak
if [ $FREEBSD -eq 0 ]; then
	echo "LDFLAGS+=$GTKLDFLAGS -lX11 -ldl" >> config.mak
else
	echo "LDFLAGS+=$GTKLDFLAGS -lX11" >> config.mak
fi
echo "HIME_TABLE_DIR=\$(datadir_r)/hime/table" >> config.mak
echo "HIME_OGG_DIR=\$(datadir_r)/hime/ogg" >> config.mak
echo "HIME_TABLE_DIR_i=\$(datadir)/hime/table" >> config.mak
echo "HIME_SCRIPT_DIR=\$(datadir_r)/hime/script" >> config.mak
echo "HIME_SCRIPT_DIR_i=\$(datadir)/hime/script" >> config.mak
echo "OPTFLAGS=$OPTFLAGS" >> config.mak
echo "DOC_DIR=\$(datadir_r)/doc/hime" >> config.mak
echo "DOC_DIR_i=\$(datadir)/doc/hime" >> config.mak
echo "HIME_ICON_DIR=\$(datadir_r)/pixmaps/hime" >> config.mak
echo "SYS_ICON_DIR=\$(datadir_r)/pixmaps" >> config.mak
echo "HIME_ICON_DIR_i=\$(datadir)/pixmaps/hime" >> config.mak
echo "HIME_BIN_DIR=\$(bindir_r)" >> config.mak
echo "FREEBSD=$FREEBSD" >> config.mak
echo "QT_IM=$QT_IM" >> config.mak
echo "QT_IM_DIR=\$(libdir)$QT_IM_DIR" >> config.mak
echo "QT4_IM=$QT4_IM" >> config.mak
if [ -z "$QT4_IM_DIR_CUSTOM" ]; then
	echo "QT4_IM_DIR=\$(libdir)$QT4_IM_DIR" >> config.mak
else
	echo "QT4_IM_DIR=\$(DESTDIR)$QT4_IM_DIR_CUSTOM" >> config.mak
fi
echo "QT3_MOC_PATH=$QT3_MOC_PATH" >> config.mak
echo "QT4_MOC_PATH=$QT4_MOC_PATH" >> config.mak
echo "QT3_QMAKE_PATH=$QT3_MOC_PATH" | sed 's/moc/qmake/' >> config.mak
echo "QT4_QMAKE_PATH=$QT4_MOC_PATH" | sed 's/moc/qmake/' >> config.mak
echo "GTK_IM=$GTK_IM" >> config.mak
echo "GTK3_IM=$GTK3_IM" >> config.mak
echo "USE_XIM=$use_xim" >> config.mak
echo "LIB=$LIB" >> config.mak
echo "MAC_OS=$MAC_OS" >> config.mak
echo "SO_FLAGS=$SO_FLAGS" >> config.mak
echo "USE_TRAY=$use_tray" >> config.mak
echo "USE_I18N=$use_i18n" >> config.mak
echo "USE_TSIN=$use_tsin" >> config.mak
echo "USE_ANTHY=$USE_ANTHY" >> config.mak
echo "USE_CHEWING=$USE_CHEWING" >> config.mak
echo "USE_UNITY=$USE_UNITY" >> config.mak
echo "INSTALL_DFSG_INCOMPATIBLE=$INSTALL_DFSG_INCOMPATIBLE" >> config.mak
echo "GIT_HAVE=$GIT_HAVE" >> config.mak
echo "GIT_HASH=$GIT_HASH" >> config.mak
echo "BUILD_MODULE=$BUILD_MODULE" >> config.mak
echo "hime_ld_run_path=\$(himelibdir)" >> config.mak
echo "gcc_ld_run_path=-Wl,-rpath,\$(hime_ld_run_path)" >> config.mak
