#!/bin/bash

if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$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-xim            : disable the support of XIM [default: enable]"
    echo "  --disable-nls            : disable Native Language Support [default: enable]"
    echo "  --disable-tsin           : disable Tsin input method [default: enable]"
    echo "  --disable-anthy          : disable Anthy input method [default: auto-detect]"
    echo "  --disable-chewing        : disable Chewing input method [default: auto-detect]"
    echo "  --disable-system-tray    : disable system tray support [default: enable]"
    echo "  --disable-appindicator   : disable Ubuntu unity tray [default: auto-detect]"
    echo "  --disable-lib64          : disable the use of lib64 dir [default: auto-detect]"
    echo "  --disable-qt5-immodule   : disable QT5 im-module [default: auto-detect]"
    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 ""
    echo "System Specific Pathes:"
    echo "  --qt5-moc-path=PATH       : specify the path of \"moc\" command for Qt 5.x"
    echo "  --qt5-im-module-path=PATH : specify the install path for Qt 5.x im-module."
    echo ""
    exit
fi

MAKE="make"
if command -v gmake >/dev/null 2>/dev/null; then
    MAKE="gmake"
fi

FREEBSD=0
if uname | grep FreeBSD | grep -v GNU/kFreeBSD >/dev/null 2>/dev/null; then
    FREEBSD=1
fi

if uname | grep OpenBSD >/dev/null 2>/dev/null; then
    FREEBSD=1
fi

SO_FLAGS="$LDFLAGS -shared -Wl,--as-needed"

prefix="/usr/local"
use_xim='Y'
use_i18n='Y'
use_tsin='Y'
use_anthy='Y'
use_chewing='Y'
use_system_tray='Y'
use_unity_tray='Y'
use_lib=''

use_qt5='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
qt5_moc_path_list="/usr/share/qt5/bin/moc /usr/lib/qt5/bin/moc /usr/bin/moc-qt5 /usr/local/bin/moc-qt5 /usr/local/bin/moc /usr/bin/moc"

QT5_IM_DIR_CUSTOM=
for opt; do
    case "$opt" in
        --prefix=*)
            prefix=$(echo "$opt" | cut -d '=' -f 2)
            ;;
        --with_gtk3 | --with-gtk=3.0)
            GTK=gtk+-3.0
            ;;
        --use_xim=*)
            use_xim=$(echo "$opt" | cut -d '=' -f 2)
            ;;
        --enable-xim)
            use_xim=Y
            ;;
        --disable-xim)
            use_xim=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_system_tray=*)
            use_system_tray=$(echo "$opt" | cut -d '=' -f 2)
            ;;
        --enable-system-tray)
            use_system_tray=Y
            ;;
        --disable-system-tray)
            use_system_tray=N
            ;;
        --use_unity_tray=*)
            use_unity_tray=$(echo "$opt" | cut -d '=' -f 2)
            ;;
        --enable-appindicator)
            use_unity_tray=Y
            ;;
        --disable-appindicator)
            use_unity_tray=N
            ;;
        --use_lib=*)
            use_lib=$(echo "$opt" | cut -d '=' -f 2)
            ;;
        --enable-lib64)
            use_lib='lib64'
            ;;
        --disable-lib64)
            use_lib='lib'
            ;;
        --use_qt5=*)
            use_qt5=$(echo "$opt" | cut -d '=' -f 2)
            ;;
        --enable-qt5-immodule)
            use_qt5=Y
            ;;
        --disable-qt5-immodule)
            use_qt5=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
            ;;
        --qt5-moc-path=*)
            QT5_MOC_PATH=$(echo "$opt" | cut -d '=' -f 2)
            ;;
        --qt5-install-to=* | --qt5-im-module-path=*)
            QT5_IM_DIR_CUSTOM=$(echo "$opt" | cut -d '=' -f 2)
            ;;
    esac
done


echo "prefix: $prefix"


echo -n ".... Testing pkg-config         :  "
if ! command -v pkg-config >/dev/null 2>&1; then
    echo "please install pkg-config. abort!"
    exit 1
else
    echo "Found: $(command -v pkg-config)"
fi


echo -n ".... Testing Xtst               :  "
if ! pkg-config --exists xtst 2>/dev/null; then
    echo "please install rpm/package xtest-devel. abort!"
    exit 1
else
    echo "Found: $(pkg-config --print-provides xtst)"
fi


echo -n ".... Testing anthy              :  "
if ! pkg-config --libs anthy >/dev/null 2>/dev/null; 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'
        ANTHY_INC="-I$(pkg-config --variable=includedir anthy) $(pkg-config --libs anthy)"
        echo "Found: $(pkg-config --print-provides anthy)"
    fi
fi

echo -n ".... Testing chewing            :  "
if ! pkg-config --libs chewing >/dev/null 2>/dev/null; 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
        if ! pkg-config --exists 'chewing >= 0.3.2' 2>/dev/null; then
            USE_CHEWING='N'
            echo "Disabled. chewing > 0.3.2 is needed to enable chewing."
        else
            USE_CHEWING='Y'
            echo "Found: $(pkg-config --print-provides chewing)"
        fi
    fi
fi

GTK=${GTK:-gtk+-2.0}
echo -n ".... Testing $GTK           :  "
if ! pkg-config --exists "$GTK"; then
    echo "Not found, trying gtk+-3.0..."
    GTK=gtk+-3.0
    if ! pkg-config --exists "$GTK"; then
        echo "$GTK or above required --cflags. abort!"
        rm -f config.mak
        exit 1
    fi
fi
echo "Found: $(pkg-config --print-provides $GTK)"

# version guards for GTK+
if [ "$GTK" = gtk+-2.0 ]; then
    if ! pkg-config --exists 'gtk+-2.0 >= 2.24.32'; then
        echo "gtk+-2.0 >= 2.24.32 is required. abort!"
        rm -f config.mak
        exit 1
    fi
fi
if [ "$GTK" = gtk+-3.0 ]; then
    if ! pkg-config --exists 'gtk+-3.0 >= 3.24.20'; then
        echo "gtk+-3.0 >= 3.24.20 is required. abort!"
        rm -f config.mak
        exit 1
    fi
fi

GTKINC=$(pkg-config --cflags "$GTK" 2>/dev/null)

if pkg-config --libs >/dev/null 2>/dev/null; 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
fi
GTKLDFLAGS=$(pkg-config --libs "$GTK" 2>/dev/null)

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

if [ "$GTK" = "gtk+-3.0" ]; then
    APPINDICATOR=appindicator3-0.1
    if ! pkg-config --exists 'gtk+-3.0 > 3.9.8'; then
        echo ".... Testing system-tray     :  Disabled. $GTK > 3.9.8 is needed to enable system-tray."
        use_system_tray=N
    fi
fi

if [ "$use_unity_tray" = "Y" ]; then
    echo -n ".... Testing appindicator       :  "
    USE_UNITY_TRAY=$(pkg-config --libs "$APPINDICATOR" 2>/dev/null)
    if [ $? != 0 ]; then
        USE_UNITY_TRAY=$(pkg-config --libs appindicator3-0.1 2>/dev/null)
        if [ $? = 0 ] && [ "$GTK" = "gtk+-2.0" ]; then
            echo "Disabled. appindicator3-0.1 is found, but is using gtk+2.0. Add option \"--with-gtk=3.0\" to enable it."
        else
            echo "Not found, unity tray is turned off."
        fi
        USE_UNITY_TRAY=N
    else
        if [ "$use_unity_tray" = "N" ]; then
            USE_UNITY_TRAY='N'
            echo "Found, but disabled."
        else
            if ! pkg-config --exists "$APPINDICATOR >= 0.4.0" 2>/dev/null; then
                echo "Disabled. appindicator > 0.4.0 is needed to enable appindicator."
                USE_UNITY_TRAY='N'
            else
                USE_UNITY_TRAY='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

echo -n ".... Testing lib                :  "
LIB='lib'
if [ -d /usr/lib64 ] && [ -d /lib64 ]; then
    LIB='lib64'
fi

if [ "$(uname -s)" = Linux ]; then
    if command -v lsb_release >/dev/null; then
        distid=$(lsb_release -is 2>/dev/null)
        case $distid in
            Ubuntu | Debian | Arch | archlinux)
                LIB='lib'
                ;;
        esac
    else
        DEBIAN_LD=$(ld -v 2>&1 | grep -i debian)
        UBUNTU_LD=$(ld -v 2>&1 | grep -i ubuntu)
        if [ "$DEBIAN_LD" != "" ] || [ "$UBUNTU_LD" != "" ]; then
            LIB='lib'
        elif [ -e /etc/arch-release ]; then
            LIB='lib'
        fi
    fi
fi

if [ -n "$use_lib" ]; then
    echo "Decteted $LIB, but set to $prefix/$use_lib"
    LIB="$use_lib"
else
    echo "Using $prefix/$LIB"
fi


# --- QT5 START ---
QT5_IM='N'
if [ -z "$QT5_IM_DIR" ]; then QT5_IM_DIR=/qt5/plugins/platforminputcontexts; fi

echo -n ".... Testing Qt 5.x             :  "
if pkg-config --libs Qt5Core Qt5Gui >/dev/null 2>/dev/null; then
    QT5_IM='Y'
fi

if [ ! -x "$QT5_MOC_PATH" ]; then
    find_in_path_list_to_variable "$(pkg-config --variable=host_bins Qt5Core)/moc $qt5_moc_path_list" "QT5_MOC_PATH"
fi

if [ "$QT5_IM" = "Y" ]; then
    if [ "$use_qt5" = "N" ]; then
        QT5_IM='N'
        echo "Found: $(pkg-config --print-provides Qt5Core), but disabled."
    else
        if [ ! -x "$QT5_MOC_PATH" ]; then
            echo "Not found, cannot find \"moc\" tool for Qt 5.x."
            echo "Hint: use the --qt5-moc-path option. abort!"
            exit 1
        fi
        echo "Found: $(pkg-config --print-provides Qt5Core)"
    fi
else
    echo "Not found, Qt 5.x immodule is turned off."
fi
# --- QT5 END ---

# --- MOC INFO START ---

if [ "$QT5_IM" = "Y" ]; then echo ".... Path of Qt5 moc            :  $QT5_MOC_PATH"; fi

# --- MOC INFO END ---

GTK_IM='N'
if [ "$GTK" = "gtk+-2.0" ]; then
    echo -n ".... Testing GTK+ 2.x immodule  :  "
    if pkg-config --libs gtk+-2.0 >/dev/null 2>/dev/null; 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  :  "
if pkg-config --libs gtk+-3.0 >/dev/null 2>/dev/null; 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
echo ".... GIT hash: $GIT_HASH"


CC="${CC:-gcc}"
CXX="${CXX:-g++}"
if [ -z "$CFLAGS" ]; then
    CFLAGS="-std=gnu17"
    CXXFLAGS="-std=gnu++17"
    OPTFLAGS="-Wall"
else
    OPTFLAGS="$CFLAGS"
fi

echo ".... Toolchains flags:"
echo "         CC/CXX = $CC/$CXX"
echo "         CFLAGS = $CFLAGS"
echo "         CXXFLAGS = $CXXFLAGS"
echo "         OPTFLAGS = $OPTFLAGS"

# build
echo "MAKE=$MAKE" >config.mak
echo "CC=$CC" >>config.mak
echo "CCX=$CC" >>config.mak
echo "CXX=$CXX" >>config.mak
echo "CCLD=$CC" >>config.mak
echo "LDFLAGS_ENV=$LDFLAGS" >>config.mak
echo "LDFLAGS=$LDFLAGS -Wl,--as-needed -lX11 -lm" >>config.mak
echo "CFLAGS=$CFLAGS $CPPFLAGS" >>config.mak
echo "CXXFLAGS=$CXXFLAGS $CPPFLAGS" >>config.mak
echo "FREEBSD=$FREEBSD" >>config.mak
if [ "$FREEBSD" -eq 0 ]; then
    echo "LDFLAGS+=$GTKLDFLAGS -lX11 -ldl" >>config.mak
else
    echo "LDFLAGS+=$GTKLDFLAGS -lX11" >>config.mak
fi
echo "OPTFLAGS=$OPTFLAGS" >>config.mak
echo "SO_FLAGS=$SO_FLAGS" >>config.mak
echo "prefix=$prefix" >>config.mak


# installation
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
hime_version="$(head -n 1 ChangeLog)"

echo "bindir=\$(DESTDIR)$bindir" >>config.mak
echo "mandir=\$(DESTDIR)$mandir" >>config.mak
echo "man1dir=\$(mandir)/man1" >>config.mak
echo "datadir=\$(DESTDIR)$datadir" >>config.mak
echo "filterdir=\$(datadir)/hime/filter" >>config.mak
echo "includedir=\$(DESTDIR)$includedir" >>config.mak
echo "libdir=\$(DESTDIR)$libdir" >>config.mak
echo "himelibdir=\$(libdir)/hime" >>config.mak
echo "hime_ld_run_path=\$(himelibdir)" >>config.mak
echo "gcc_ld_run_path=-Wl,-rpath,\$(hime_ld_run_path)" >>config.mak

echo "HIME_BIN_DIR=\$(bindir)" >>config.mak
echo "HIME_DEFAULT_ICON_DIR=\$(datadir)/pixmaps/hime" >>config.mak
echo "HIME_OGG_DIR=\$(datadir)/hime/ogg" >>config.mak
echo "HIME_SCRIPT_DIR=\$(datadir)/hime/script" >>config.mak
echo "HIME_TABLE_DIR=\$(datadir)/hime/table" >>config.mak
echo "HIME_VERSION=$hime_version" >>config.mak

echo "DOC_DIR=\$(datadir)/doc/hime" >>config.mak
echo "SYS_ICON_DIR=\$(datadir)/pixmaps" >>config.mak

echo "INSTALL_DFSG_INCOMPATIBLE=$INSTALL_DFSG_INCOMPATIBLE" >>config.mak

# configure script flags
echo "USE_XIM=$use_xim" >>config.mak
echo "USE_SYSTEM_TRAY=$use_system_tray" >>config.mak
echo "USE_UNITY_TRAY=$USE_UNITY_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 "ANTHY_INC=$ANTHY_INC" >>config.mak
echo "USE_CHEWING=$USE_CHEWING" >>config.mak
echo "LIB=$LIB" >>config.mak

# QT
echo "QT5_IM=$QT5_IM" >>config.mak
if [ -z "$QT5_IM_DIR_CUSTOM" ]; then
    echo "QT5_IM_DIR=$(pkg-config --define-variable=prefix=$\(DESTDIR\)/\$\(prefix\) --variable=libdir Qt5Core)$QT5_IM_DIR" >>config.mak
else
    echo "QT5_IM_DIR=\$(DESTDIR)$QT5_IM_DIR_CUSTOM" >>config.mak
fi
echo "QT5_MOC_PATH=$QT5_MOC_PATH" >>config.mak
echo "QT5_QMAKE_PATH=$QT5_MOC_PATH" | sed 's/moc/qmake/' >>config.mak

# GTK
echo "GTK=$GTK" >>config.mak
echo "GTKINC=$GTKINC" >>config.mak
echo "GTK_IM=$GTK_IM" >>config.mak
echo "GTK3_IM=$GTK3_IM" >>config.mak

# git
echo "GIT_HAVE=$GIT_HAVE" >>config.mak
echo "GIT_HASH=$GIT_HASH" >>config.mak
