#!/bin/sh
#
# FLTK configuration utility.
#
# Copyright 2000-2025 by Bill Spitzak and others.
# Original version Copyright 2000 by James Dean Palmer
# Adapted by Vincent Penne and Michael Sweet
#
# This library is free software. Distribution and use rights are outlined in
# the file "COPYING" which should have been included with this file.  If this
# file is missing or damaged, see the license at:
#
#      https://www.fltk.org/COPYING.php
#
# Please see the following page on how to report bugs and issues:
#
#      https://www.fltk.org/bugs.php
#

# Variables and constants generated by CMake:

# version numbers
MAJOR_VERSION=@FLTK_VERSION_MAJOR@
MINOR_VERSION=@FLTK_VERSION_MINOR@
PATCH_VERSION=@FLTK_VERSION_PATCH@

VERSION="$MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION"
APIVERSION="$MAJOR_VERSION.$MINOR_VERSION"

# compiler names
CC="@CC@"
CXX="@CXX@"

# flags for C++ compiler:
ARCHFLAGS="@ARCHFLAGS@"
CFLAGS="@CFLAGS@ @LARGEFILE@ @PTHREAD_FLAGS@"
CXXFLAGS="@CXXFLAGS@ @LARGEFILE@ @PTHREAD_FLAGS@"
LDFLAGS="@LDFLAGS@"
LDLIBS="@LIBS@"
OPTIM="@OPTIM@"
CAIROFLAGS="@CAIROFLAGS@"

# BINARY_DIR: this is only set for CMake builds in the CMake build directory,
# otherwise it is an empty string (for instance in the install directory)
BINARY_DIR=@BINARY_DIR@

prefix="@prefix@"
exec_prefix="@exec_prefix@"
exec_prefix_set=no
includedir="@includedir@"
libdir="@libdir@"
srcdir="@srcdir@"

# BEGIN fltk-config code

# Check for local invocation (in FLTK source folder), and update paths accordingly...
selfdir=`dirname "$0"`
if test -f "$selfdir/FL/Fl_Window.H"; then
    includedir="$selfdir"
    libdir="$selfdir/lib"
fi

# Libraries to link with (must be after 'prefix' stuff and after check for local invocation)
LIBNAME="@LIBNAME@"
DSONAME="@DSONAME@"
DSOLINK="@DSOLINK@"
IMAGELIBS="@IMAGELIBS@"
STATICIMAGELIBS="@STATICIMAGELIBS@"
SHAREDSUFFIX="@SHAREDSUFFIX@"
CAIROLIBS="@CAIROLIBS@"
GLLIBS="@GLLIBS@"

# Check bundled image libraries in source tree
if test -f "$libdir/libfltk_jpeg.a" -a -d $includedir/jpeg; then
    CFLAGS="-I$includedir/jpeg $CFLAGS"
    CXXFLAGS="-I$includedir/jpeg $CXXFLAGS"
fi

if test -f "$libdir/libfltk_z.a" -a -d $includedir/zlib; then
    CFLAGS="-I$includedir/zlib $CFLAGS"
    CXXFLAGS="-I$includedir/zlib $CXXFLAGS"
fi

if test -f "$libdir/libfltk_png.a" -a -d $includedir/png; then
    CFLAGS="-I$includedir/png $CFLAGS"
    CXXFLAGS="-I$includedir/png $CXXFLAGS"
fi

# Check bundled image libraries in installation folder.
# Note: jpeg, png, and zlib headers are installed in FL/images
if test -d "$includedir/FL/images"; then
    CFLAGS="-I$includedir/FL/images $CFLAGS"
    CXXFLAGS="-I$includedir/FL/images $CXXFLAGS"
fi

# Cairo support: *append* CAIROFLAGS
if test -n "$CAIROFLAGS"; then
    CFLAGS="$CFLAGS $CAIROFLAGS"
    CXXFLAGS="$CXXFLAGS $CAIROFLAGS"
fi

usage ()
{
    echo "Usage: fltk-config [OPTIONS]
Options:
    [--version]
    [--api-version]

Options telling what we are doing:
    [--use-gl]        use GL
    [--use-images]    use extra image formats (PNG, JPEG)
    [--use-glut]      use glut compatibility layer
    [--use-forms]     use forms compatibility layer
    [--use-cairo]     use cairo graphics lib

Options telling what information we request:
    [--cc]            return C compiler used to compile FLTK
    [--cxx]           return C++ compiler used to compile FLTK
    [--optim]         return compiler optimization used to compile FLTK
    [--cflags]        return flags to compile C using FLTK
    [--cxxflags]      return flags to compile C++ using FLTK
    [--ldflags]       return flags to link against FLTK
    [--ldstaticflags] return flags to link against static FLTK library
                                           even if there are DSOs installed
    [--libs]          return FLTK libraries full path for dependencies
    [--prefix]        return FLTK install time --prefix directory
    [--includedir]    return FLTK install time include directory

Options to compile and link an application:
    [-g]                        compile the program with debugging information
    [-Dname[=value]]            compile the program with the given define
    [--compile program.cxx ..]  compile and link the program with optional
                                .. compiler flags, source files, and libraries
    [--link flags and libs]     additional linker flags and libraries
                                .. for --compile, added after all other libs
    [--post program]            prepare the program for desktop use (macOS)
"
    exit $1
}

if test $# -eq 0; then
    usage 1
fi

# variables for parsing and generation of the commandline
compile=
post=
debug=
flags=
files=
use_link=
link=

# Parse command line options
while test $# -gt 0
do
    case $1 in
        --version)
            echo $VERSION
            ;;
        --api-version)
            echo $APIVERSION
            ;;
        --cc)
            echo $CC
            ;;
        --cxx)
            echo $CXX
            ;;
        --optim)
            echo_optim=yes
            ;;
        --use-gl | --use-glut)
            use_gl=yes
            ;;
        --use-forms)
            use_forms=yes
            ;;
        --use-images)
            use_images=yes
            ;;
        --use-cairo)
            use_cairo=yes
            ;;
        --cflags)
            echo_cflags=yes
            ;;
        --cxxflags)
            echo_cxxflags=yes
            ;;
        --ldflags)
            echo_ldflags=yes
            ;;
        --ldstaticflags)
            echo_ldstaticflags=yes
            ;;
        --libs)
            echo_libs=yes
            ;;
        --prefix)
            echo_prefix=yes
            ;;
        --includedir)
            echo_includedir=yes
            ;;
        -g)
            debug=-g
            ;;
        -D*)
            CXXFLAGS="$CXXFLAGS $1"
            ;;
        --compile)
            compile="$2"
            shift
            ;;
        --post)
            post="$2"
            shift
            ;;
        --link)
            # linker flags and libs, allowed only after "--compile"
            if test -n "$compile"; then
                use_link=yes
            else
                echo_help=yes
            fi
            ;;
        -*)
            # additional compiler flags, allowed only after "--compile"
            if test -n "$compile"; then
                if test -n "$use_link"; then
                    link="$link $1"                     # flags and libs after "--link"
                else
                    flags="$flags $1"                   # compiler flags like "-I..."
                fi
            else
                echo_help=yes
            fi
            ;;
        *)
            # additional source files or libs, allowed only after "--compile"
            if test -n "$compile"; then
                if test -n "$use_link"; then
                    link="$link $1"                     # linker flag or library
                else
                    files="$files $1"                   # other (source) files
                fi
            else
                echo_help=yes
            fi
            ;;
    esac
    shift
done

if test "$includedir" != /usr/include; then
    includes="-I$includedir"
else
    includes=
fi

# prepend build directory for fltk-config in CMake build folder
if test -n "$BINARY_DIR"; then
    includes="-I$BINARY_DIR $includes"
fi

if test "$libdir" != /usr/lib -a "$libdir" != /usr/lib32; then
    libs="-L$libdir"
else
    libs=
fi

# Calculate needed libraries
LDSTATIC="$libdir/libfltk.a $LDLIBS"
LDLIBS="-lfltk$SHAREDSUFFIX $LDLIBS"

if test x$use_forms = xyes; then
    LDLIBS="-lfltk_forms$SHAREDSUFFIX $LDLIBS"
    LDSTATIC="$libdir/libfltk_forms.a $LDSTATIC"
fi

if test x$use_gl = xyes; then
    LDLIBS="-lfltk_gl$SHAREDSUFFIX $GLLIBS $LDLIBS"
    LDSTATIC="$libdir/libfltk_gl.a $GLLIBS $LDSTATIC"
fi

if test x$use_images = xyes; then
    LDLIBS="-lfltk_images$SHAREDSUFFIX $IMAGELIBS $LDLIBS"
    LDSTATIC="$libdir/libfltk_images.a $STATICIMAGELIBS $LDSTATIC"
fi

if test x$use_cairo = xyes -a -n "$CAIROLIBS"; then
    LDLIBS="$CAIROLIBS $LDLIBS"
    LDSTATIC="$CAIROLIBS $LDSTATIC"
fi

LDLIBS="$DSOLINK $LDFLAGS $libs $LDLIBS"
LDSTATIC="$LDFLAGS $LDSTATIC"

# Answer to user requests
if test -n "$echo_help"; then
    usage 1
fi

if test -n "$compile"; then
    case "$compile" in
        *.cxx)
            prog="`basename \"$compile\" .cxx`"
            ;;
        *.cpp)
            prog="`basename \"$compile\" .cpp`"
            ;;
        *.cc)
            prog="`basename \"$compile\" .cc`"
            ;;
        *.C)
            prog="`basename \"$compile\" .C`"
            ;;
        *)
            echo "ERROR: Unknown/bad C++ source file extension on \"$compile\"!"
            exit 1
            ;;
    esac

    post="$prog"

    command="$CXX $ARCHFLAGS $includes $CXXFLAGS $debug -o $prog $flags $compile $files $LDSTATIC $link"

    echo $command
    $command || exit 1
fi

if test -n "$post"; then
    running=`uname`
    if test "$running" = "Darwin"; then
        # if FLTK targets macOS + X11, apps need not be bundled
        if test `echo $LDLIBS | fgrep -c -e " -lX11"` = 1; then
            running=""
        fi
    fi
    case $running in
        Darwin)
            echo Creating "'$post.app'" bundle for desktop...
            id=`echo $post | tr ' ' '_'`

            # Make the bundle directory and move the executable there
            rm -rf "$post.app/Contents/MacOS"
            mkdir -p "$post.app/Contents/MacOS"
            mv "$post" "$post.app/Contents/MacOS"

            # Make a shell script that runs the bundled executable
            echo "#!/bin/sh" >"$post"
            echo 'dir="`dirname \"$0\"`"' >>"$post"
            echo 'exec "$dir/'"$post.app/Contents/MacOS/$post"'" "$@"' >>"$post"
            chmod +x "$post"

            # Make the simplest Info.plist needed for an application
            cat >"$post.app/Contents/Info.plist" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<plist version="0.9">
    <dict>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleExecutable</key>
        <string>$post</string>
        <key>CFBundleIdentifier</key>
        <string>org.fltk.$id</string>
        <key>CFBundleName</key>
        <string>$post</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>NSHighResolutionCapable</key>
        <true/>
    </dict>
</plist>
EOF
            ;;
    esac
fi

if test "$echo_cflags" = "yes"; then
    echo $includes $CFLAGS
fi

if test "$echo_cxxflags" = "yes"; then
    echo $includes $CXXFLAGS
fi

if test "$echo_optim" = "yes"; then
    echo $OPTIM
fi

if test "$echo_ldflags" = "yes"; then
    my_libs=
    libdirs="$libs"

    for i in $LDLIBS ; do
        if test $i != "-L$libdir" ; then
            if test -z "$my_libs" ; then
                my_libs="$i"
            else
                my_libs="$my_libs $i"
            fi
        fi
    done
    echo $libdirs $my_libs
fi

if test "$echo_ldstaticflags" = "yes"; then
    echo $LDSTATIC
fi

if test "$echo_libs" = "yes"; then
    USELIBS="$libdir/libfltk.a"

    if test x$use_forms = xyes; then
        USELIBS="$libdir/libfltk_forms.a $USELIBS"
    fi

    if test x$use_gl = xyes; then
        USELIBS="$libdir/libfltk_gl.a $USELIBS"
    fi

    if test x$use_images = xyes; then
        USELIBS="$libdir/libfltk_images.a $USELIBS"

        for lib in fltk_jpeg fltk_png fltk_z; do
            if test -f "$libdir/lib$lib.a"; then
                USELIBS="$libdir/lib$lib.a $USELIBS"
            fi
        done
    fi

    echo $USELIBS
fi

if test "$echo_prefix" = "yes"; then
    echo $prefix
fi

if test "$echo_includedir" = "yes"; then
    echo $includedir
fi
