#!/usr/bin/env bash
#
# Copyright 2009-2014 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() {
    cat <<- EOT
Usage:
    ./configure [OPTIONS]...

Options:
    -h, --help              Print this help and exit.
    --assume-pkgs           Turn off package version checking.
    --build=DIR             Build secondary files in DIR.
    --debug | --release	    Build executable for debugging or release.
                            [--release]
    --profile               Enable profiling support during C compile & link.
    --prefix=PREFIX         Prepend PREFIX to program installation paths.
                            [/usr/local]
    --lib=LIBNAME           Set system library directory name to LIBNAME
                            (usually 'lib' or 'lib64').
                            [lib]
    --libdir LIBNAME        Equivalent to --lib=LIBNAME
    --libexec=LIBEXECDIR    Set utility executable to LIBEXECDIR
                            (usually 'PREFIX/libexec/shotwell').
                            [PREFIX/libexec/shotwell]
    --libexec LIBEXECDIR    Equivalent to --libexec=LIBEXECDIR

    --define=SYMBOL         Define a symbol for the Vala compiler.


    --disable-schemas-compile
                            Disable compiling the GSettings schema.
    --disable-gsettings-convert-install
                            Disable installing the gsettings-data-convert file.
    --disable-desktop-update
                            Disable desktop database update.
    --disable-desktop-validate
                            Disable validation of .desktop files.
    --disable-icon-update
                            Disable icon cache update.
    --enable-build-for-glade
                            Enable build for Glade-related development.
    --disable-help-install
                            Disable installation of online help.
    --disable-extra-plugins-install
                            Disable installation of extra (non-core) plugins.
    --install-headers
                            Install headers and VAPI files (developers only).
    --unity-support
                            Enable support for progress bars in the Unity launcher.
EOT
}

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
                                shift
                                value=$1
                            fi
                            
                            variables="${variables}PREFIX=$value\n"
                            ;;

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

        --libexec)          if [ ! $value ]
                            then
                                shift
                                value=$1
                            fi
                            
                            variables="${variables}LIBEXECDIR=$value\n"
                            ;;

        --assume-pkgs)      variables="${variables}ASSUME_PKGS=1\n"
                            ;;
        
        --build)            if [ ! $value ]
                            then
                                shift
                                value=$1
                            fi
                            
                            variables="${variables}BUILD_DIR=$value\n"
                            ;;
        
        --debug)            variables="${variables}BUILD_RELEASE=\nBUILD_DEBUG=1\n"
                            ;;
        
        --profile)          variables="${variables}PROFILE_FLAGS=-pg\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-desktop-validate)       variables="${variables}DISABLE_DESKTOP_VALIDATE=1\n"
                                          ;;

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

        --enable-build-for-glade)         variables="${variables}ENABLE_BUILD_FOR_GLADE=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"
                                          ;;
        
        --enable-* | --disable-*)
        
                            echo WARNING: unrecognized option: $option
                            ;;
        
        *)                  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 
    pkg-config --atleast-version 2.4 libgphoto2
    if [ $? == 1 ]
    then
        printf "No compatible libGPhoto installation found. Configuration cannot continue.\n";
        exit 1
    else
        printf "Detected libGPhoto 2.4.x - using default code path.\n";
    fi
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"