#!/usr/bin/env bash
#
# Copyright 2009-2013 Yorba Foundation
#
# This software is licensed under the GNU LGPL (version 2.1 or later).
# See the COPYING file in this distribution.

CONFIG_IN=configure.mk

configure_help() {
    printf "\nUsage:\n"
    printf "\t./configure [OPTIONS]...\n"
    printf "\n"
    printf "Options:\n"
    printf "\t-h, --help\t\tPrint this help and exit.\n"
    printf "\t--enable-tests\t\tEnable Shotwell to run automated tests.\n"
    printf "\t--assume-pkgs\t\tTurn off package version checking.\n"
    printf "\t--build=DIR\t\tBuild secondary files in DIR.\n"
    printf "\t--debug | --release\tBuild executable for debugging or release.\n"
    printf "\t\t\t\t[--release]\n"
    printf "\t--prefix=PREFIX\t\tPrepend PREFIX to program installation paths.\n"
    printf "\t\t\t\t[/usr/local]\n"
    printf "\t--lib=LIBNAME\t\tSet system library directory name to LIBNAME\n\t\t\t\t(usually 'lib' or 'lib64').\n"
    printf "\t\t\t\t[lib]\n"
    printf "\t--define=SYMBOL\t\tDefine a symbol for the Vala compiler.\n"
    printf "\n\n"
    printf "\t--disable-schemas-compile\n"
    printf "\t\t\t\tDisable compiling the GSettings schema.\n"
    printf "\t--disable-gsettings-convert-install\n"
    printf "\t\t\t\tDisable installing the gsettings-data-convert file.\n"
    printf "\t--disable-desktop-update\n"
    printf "\t\t\t\tDisable desktop database update.\n"
    printf "\t--disable-icon-update\n"
    printf "\t\t\t\tDisable icon cache update.\n"
    printf "\t--enable-build-for-glade\n"
    printf "\t\t\t\tEnable build for Glade-related development.\n"
    printf "\t--disable-help-install\n"
    printf "\t\t\t\tDisable installation of online help.\n"
    printf "\t--disable-extra-plugins-install\n"
    printf "\t\t\t\tDisable installation of extra (non-core) plugins.\n"
    printf "\t--install-headers\n"
    printf "\t\t\t\tInstall headers and VAPI files (developers only).\n"
    printf "\t--unity-support\n"
    printf "\t\t\t\tEnable support for progress bars in the Unity launcher.\n"
    printf "\n"
}

abort() {
    printf "%s: Invalid argument %s\n" $0 $1
    configure_help
    exit 1
}

while [ $# != 0 ]
do
    option=`echo $1 | sed 's/=.*//'`
    if [ `echo $1 | grep '='` ]
    then
        value=`echo $1 | sed 's/.*=//'`
    fi

    case $option in
        -h | --help)        configure_help
                            exit 0
                            ;;
        
        --prefix)           if [ ! $value ]
                            then
                                abort $1
                            fi
                            
                            variables="${variables}PREFIX=$value\n"
                            ;;

        --lib)              if [ ! $value ]
                            then
                                abort $1
                            fi
                            
                            variables="${variables}LIB=$value\n"
                            ;;

        --assume-pkgs)      variables="${variables}ASSUME_PKGS=1\n"
                            ;;
        
        --build)            if [ ! $value ]
                            then
                                abort $1
                            fi
                            
                            variables="${variables}BUILD_DIR=$value\n"
                            ;;
        
        --debug)            variables="${variables}BUILD_RELEASE=\nBUILD_DEBUG=1\n"
                            ;;
        
        --release)          variables="${variables}BUILD_DEBUG=\nBUILD_RELEASE=1\n"
                            ;;
        
        --define)           variables="${variables}USER_VALAFLAGS+=--define=$value\n"
                            ;;
                            
        --disable-schemas-compile)        variables="${variables}DISABLE_SCHEMAS_COMPILE=1\n"
                                          ;;
        
        --disable-gsettings-convert-install)    variables="${variables}DISABLE_GSETTINGS_CONVERT_INSTALL=1\n"
                                                ;;
        
        --disable-desktop-update)         variables="${variables}DISABLE_DESKTOP_UPDATE=1\n"
                                          ;;

        --disable-icon-update)            variables="${variables}DISABLE_ICON_UPDATE=1\n"
                                          ;;

        --enable-build-for-glade)         variables="${variables}ENABLE_BUILD_FOR_GLADE=1\n"
                                          ;;
        --enable-tests)                   variables="${variables}ENABLE_TESTS=1\n"
                                          ;;
        --disable-help-install)           variables="${variables}DISABLE_HELP_INSTALL=1\n"
                                          ;;
        --disable-extra-plugins-install)  variables="${variables}DISABLE_EXTRA_PLUGINS_INSTALL=1\n"
                                          ;;
        
        --install-headers)                variables="${variables}INSTALL_HEADERS=1\n"
                                          ;;
        
        --unity-support)                  variables="${variables}UNITY_SUPPORT=1\n"
                                          ;;
        
        *)                  if [ `echo $option | grep '\-\-'` ]
                            then
                                # we've hit a bogus '--' -type argument, don't accept it.
                                abort $option
                            fi
                            
                            # this argument isn't for us; pass it on to the makefile phase.
                            variables="${variables}${option}=${value}\n"
                            ;;
    esac

    value=""
    shift
done

# detect version of libgphoto2 the compilation host has installed
pkg-config --atleast-version 2.5 libgphoto2
if [ $? == 1 ]
then 
    printf "Detected libGPhoto 2.4.x - using default code path.\n";
else
    printf "Detected libGPhoto 2.5.x - using 2.5-aware code path.\n";
    variables="${variables}WITH_GPHOTO_25=1\n"
fi

rm -f $CONFIG_IN
if [ $variables ]
then
    echo -e -n $variables > $CONFIG_IN
fi
echo "CONFIG_IN=${CONFIG_IN}" >> $CONFIG_IN

printf "Configured.  Type 'make' to build, 'make install' to install.\n"
