#!/bin/sh

# Copyright (C) 2008-2025 Cosmin Truta.
#
# This software is distributed under the zlib license.
# Please see the accompanying LICENSE file.

trap "rm -f conftest* core a.out; exit 1" 1 2 3 15

echo "WARNING:"
echo "    This script has been deprecated in favour of the CMake file."
echo "    It is currently being maintained on a best-effort basis only."
echo "    Please use the CMake file, if possible."
echo ""

LC_ALL=C
LANG=C
LANGUAGE=C
export LC_ALL LANG LANGUAGE

prefix="${prefix-/usr/local}"
exec_prefix="${exec_prefix-\$(prefix)}"
bindir="${bindir-\$(exec_prefix)/bin}"
mandir="${mandir-\$(prefix)/man}"
man1dir="${man1dir-\$(mandir)/man1}"

cc="${CC-cc}"
#cc="${CC-clang}"
#cc="${CC-gcc}"
cflags="$CFLAGS"

enable_debug=0

with_system_libpng=0
with_system_zlib=0

with_preconfigured_libpng=1
with_preconfigured_zlib=0

top_srcdir=`dirname "$0"`
libpng_srcdir="$top_srcdir/third_party/libpng"
zlib_srcdir="$top_srcdir/third_party/zlib"

unique_file="./src/optipng/optipng.c"

for arg in "$@"
do
    case "$arg" in
    -- )
        option="$arg"
        ;;
    --* )
        option=`expr "X$arg" : 'X-\(.*\)'`
        ;;
    * )
        option="$arg"
        ;;
    esac
    case "$arg" in
    *=* )
        optarg=`expr "X$arg" : 'X[^=]*=\(.*\)'`
        ;;
    * )
        optarg=""
        ;;
    esac
    case "$option" in
    -help | -hel | -he | -[h?] )
        echo "Usage:"
        echo "    $0 [options]"
        echo "Options:"
        echo "    -?, -h, --help          Show this help"
        echo "Installation directories:"
        echo "    --prefix=PREFIX         Install architecture-independent files in PREFIX"
        echo "                            [default: $prefix]"
        echo "    --exec-prefix=EPREFIX   Install architecture-dependent files in EPREFIX"
        echo "                            [default: PREFIX]"
        echo "    --bindir=DIR            Install executable in DIR [default: EPREFIX/bin]"
        echo "    --mandir=DIR            Install manual in DIR [default: PREFIX/man]"
        echo "Optional features:"
        echo "    --enable-debug          Enable debug build flags and run-time checks"
        echo "Optional packages:"
        echo "    --with-system-libs      Use all system-supplied libraries (details below)"
        echo "    --with-system-libpng    Use the system-supplied libpng"
        echo "                            [default: false]"
        echo "    --with-system-zlib      Use the system-supplied zlib"
        echo "                            [default: with-system-libpng]"
        echo "Environment variables:"
        echo "    CC                      C compiler command"
        echo "    LD                      Linker command"
        echo "    AR                      Library archiver command"
        echo "    RANLIB                  Library indexer/randomizer command"
        echo "    CFLAGS                  C compiler flags (e.g. -O3)"
        echo "    CPPFLAGS                C preprocessor flags (e.g. -I DIR)"
        echo "    LDFLAGS                 Linker flags (e.g. -L DIR)"
        echo "    ARFLAGS                 Library archiver flags (e.g. rcu)"
        echo "    LIBS                    Additional libraries (e.g. -lfoo)"
        exit 0
        ;;
    -prefix | -prefi | -pref | -pre | -pr | -p )
        prefix="$2"
        shift
        ;;
    -prefix=* | -prefi=* | -pref=* | -pre=* | -pr=* | -p=* )
        prefix="$optarg"
        ;;
    -exec-prefix | -exec_prefix | -exec-prefi | -exec_prefi \
    | -exec-pref |  -exec_pref | -exec-pre | -exec_pre \
    | -exec-pr | -exec_pr | -exec-p | -exec_p | exec- | -exec_ \
    | -exec | -exe | -ex | -e )
        exec_prefix="$2"
        shift
        ;;
    -exec-prefix=* | -exec_prefix=* | -exec-prefi=* | -exec_prefi=* \
    | -exec-pref=* |  -exec_pref=* | -exec-pre=* | -exec_pre=* \
    | -exec-pr=* | -exec_pr=* | -exec-p=* | -exec_p=* | exec-=* | -exec_=* \
    | -exec=* | -exe=* | -ex=* | -e=* )
        exec_prefix="$optarg"
        ;;
    -bindir | -bindi | -bind | -bin | -bi | -b )
        bindir="$2"
        shift
        ;;
    -bindir=* | -bindi=* | -bind=* | -bin=* | -bi=* | -b=* )
        bindir="$optarg"
        ;;
    -mandir | -mandi | -mand | -man | -ma | -m )
        mandir="$2"
        shift
        ;;
    -mandir=* | -mandi=* | -mand=* | -man=* | -ma=* | -m=* )
        mandir="$optarg"
        ;;
    -enable-debug | -enable-debu | -enable-deb | -enable-de | -enable-d )
        enable_debug=1
        ;;
    -disable-debug | -disable-debu | -disable-deb | -disable-de | -disable-d )
        enable_debug=0
        ;;
    -with-system-libs )
        with_system_libpng=1
        with_system_zlib=1
        ;;
    -without-system-libs )
        with_system_zlib=0
        with_system_libpng=0
        ;;
    -with-system-libpng )
        with_system_libpng=1
        # Must use the system-supplied zlib with the system-supplied libpng.
        with_system_zlib=1
        ;;
    -without-system-libpng )
        with_system_libpng=0
        ;;
    -with-system-zlib )
        with_system_zlib=1
        ;;
    -without-system-zlib )
        with_system_zlib=0
        # Can't use the system-supplied libpng without the system-supplied zlib.
        with_system_libpng=0
        ;;
    * )
        echo "$0: error: unknown option: '$arg'"
        echo "Type \"$0 --help\" for help"
        exit 64  # EX_USAGE
        ;;
    esac
done

test -f "$0" || {
    echo "$0: error: cannot find myself; rerun with an absolute file name"
    exit 1
}

if test "$with_system_zlib" -eq 0 || test "$with_system_libpng" -eq 0
then
    test -r "$unique_file" || {
        echo "$0: error: no such file: '$unique_file'"
        echo "$0: note: out-of-tree builds are not supported in this configuration"
        echo "$0: note: please run: 'cd $top_srcdir && ./configure${*:+ $*}'"
        exit 1
    }
fi

test=conftest$$
cat > $test.c <<EOM
int hello() { return 42; }
EOM

gccish=0
case "$cc" in
*gcc* | *clang* )
    echo "Checking for $cc..."
    if ("$cc" -c $cflags $test.c) 2>/dev/null
    then
        gccish=1
    fi
    ;;
esac

rm -f $test.c $test.o

if test "$gccish" -ne 0
then
    CC="${CC-$cc}"
    CFLAGS="${CFLAGS--O2 -Wall -Wextra -Wundef}"
else
    CC="${CC-cc}"
    CFLAGS="${CFLAGS--O}"
fi

if test "$enable_debug" -ne 0
then
    CPPFLAGS="$CPPFLAGS -DDEBUG -D_DEBUG -DPNGX_DEBUG"
    CFLAGS="$CFLAGS -g"
    LDFLAGS="$LDFLAGS -g"
fi

if test "$with_system_libpng" -ne 0
then
    USE_SYSTEM_LIBPNG_TRUE=""
    USE_SYSTEM_LIBPNG_FALSE="#"
    echo "Checking for system libpng..."
    test=conftest$$
cat > $test.c <<EOM
#include <png.h>
#if PNG_LIBPNG_VER < 10635
#error This program requires libpng version 1.6.35 or higher
#endif
int dummy;
EOM
    ("$CC" -c $CPPFLAGS $CFLAGS $test.c) 2>/dev/null
    status=$?
    rm -f $test.c $test.o
    if test $status -ne 0
    then
        echo "$0: error: missing libpng or incorrect libpng version"
        echo "$0: note: libpng version 1.6.35 or higher is required"
        exit 1
    fi
else
    USE_SYSTEM_LIBPNG_TRUE="#"
    USE_SYSTEM_LIBPNG_FALSE=""
    if test "$with_preconfigured_libpng" -ne 0
    then
        echo "Using pre-configured libpng..."
        libpng_preconfig_makefile=scripts/makefile.gcc
        test -f "$libpng_srcdir/$libpng_preconfig_makefile" || {
            echo "$0: error: bad or missing makefile: '$libpng_srcdir/$libpng_preconfig_makefile'"
            exit 1
        }
        libpng_libconf_file=../vendoring/libpng_extras/pnglibconf_optipng_nosimd.h
        test -f "$libpng_srcdir/$libpng_libconf_file" || {
            echo "$0: error: bad or missing libconf file: '$libpng_srcdir/$libpng_libconf_file'"
            exit 1
        }
    fi
    if test "$with_preconfigured_libpng" -ne 0
    then
        sed_preconfig_libpng=""
        if test "$CC"
        then
            sed_preconfig_libpng="
                $sed_preconfig_libpng
                s|^CC *=.*|CC = $CC|
            "
        fi
        if test "$CFLAGS"
        then
            sed_preconfig_libpng="
                $sed_preconfig_libpng
                s|^CFLAGS *=.*|CFLAGS = $CFLAGS|
            "
        fi
        if test "$CPPFLAGS"
        then
            sed_preconfig_libpng="
                $sed_preconfig_libpng
                s|^CPPFLAGS *=.*|CPPFLAGS = $CPPFLAGS|
            "
        fi
        if test "$LD"
        then
            sed_preconfig_libpng="
                $sed_preconfig_libpng
                s|^LD *=.*|LD = $LD|
            "
        fi
        if test "$LDFLAGS"
        then
            sed_preconfig_libpng="
                $sed_preconfig_libpng
                s|^LDFLAGS *=.*|LDFLAGS = $LDFLAGS|
            "
        fi
        if test "$AR$ARFLAGS"
        then
            AR="${AR-ar}"
            ARFLAGS="${ARFLAGS-cru}"
            sed_preconfig_libpng="
                $sed_preconfig_libpng
                s|^AR *=.*|AR = $AR|
                s|^ARFLAGS *=.*|ARFLAGS = $ARFLAGS|
                s|^AR_\([A-Z]*\) *=.*|AR_\1 = $AR $ARFLAGS|
            "
        fi
        if test "$RANLIB"
        then
            sed_preconfig_libpng="
                $sed_preconfig_libpng
                s|^RANLIB *=.*|RANLIB = $RANLIB|
            "
        fi
        sed "$sed_preconfig_libpng" \
            "$libpng_srcdir/$libpng_preconfig_makefile" > "$libpng_srcdir/Makefile"
        LIBPNG_MK=Makefile
        # The pre-configured makefiles in libpng don't do distclean.
        LIBPNG_DISTCLEAN="clean"
        LIBPNG_DISTCLEAN_XCMD="\$(RM_F) \$(LIBPNG_MK)"
    else
        echo "Configuring libpng..."
        (cd "$libpng_srcdir" && ./configure --enable-static --disable-shared) || {
            echo "$0: error: could not configure: libpng"
            exit 1
        }
    fi
fi

if test "$with_system_zlib" -ne 0
then
    USE_SYSTEM_ZLIB_TRUE=""
    USE_SYSTEM_ZLIB_FALSE="#"
    echo "Checking for system zlib..."
    test=conftest$$
cat > $test.c <<EOM
#include <zlib.h>
#if ZLIB_VERNUM < 0x1280
#error This program requires zlib version 1.2.8 or higher.
#endif
int dummy;
EOM
    ("$CC" -c $CPPFLAGS $CFLAGS $test.c) 2>/dev/null
    status=$?
    rm -f $test.c $test.o
    if test $status -ne 0
    then
        echo "$0: error: missing zlib or incorrect zlib version"
        echo "$0: note: zlib version 1.2.8 or higher is required"
        exit 1
    fi
else
    USE_SYSTEM_ZLIB_TRUE="#"
    USE_SYSTEM_ZLIB_FALSE=""
    ZLIB_MK=Makefile
    case "$cc" in
    *mingw*-cc* | *mingw*-gcc* )
        ZLIB_MK=win32/Makefile.gcc
        ;;
    *djgpp*-cc* | *djgpp*-gcc* )
        ZLIB_MK=msdos/Makefile.dj2
        ;;
    * )
        case `(uname -s) 2>/dev/null || echo unknown` in
        mingw* | MINGW* | MSYS* | windows* | WINDOWS* )
            ZLIB_MK=win32/Makefile.gcc
            ;;
        *djgpp | *DJGPP | *dos | *DOS )
            ZLIB_MK=msdos/Makefile.dj2
            ;;
        esac
        ;;
    esac
    case "$ZLIB_MK" in
    win32/Makefile.gcc | msdos/Makefile.dj2 )
        with_preconfigured_zlib=1
        # These pre-configured makefiles do not have the distclean target.
        ZLIB_DISTCLEAN=clean
        ;;
    * )
        with_preconfigured_zlib=0
        ZLIB_DISTCLEAN=distclean
        ;;
    esac
    if test "$with_preconfigured_zlib" -ne 0
    then
        echo "Using pre-configured zlib..."
        test -f "$zlib_srcdir/$ZLIB_MK" || {
            echo "$0: error: bad or missing makefile: '$zlib_srcdir/$ZLIB_MK'"
            exit 1
        }
    else
        echo "Configuring zlib..."
        (cd "$zlib_srcdir" && ./configure --static) || {
            echo "$0: error: could not configure: zlib"
            exit 1
        }
    fi
fi

sed_config="
    s|@srcdir@|.|g
    s|@top_srcdir@|\$(srcdir)/../..|g
    s|@prefix@|$prefix|g
    s|@exec_prefix@|$exec_prefix|g
    s|@bindir@|$bindir|g
    s|@mandir@|$mandir|g
    s|@man1dir@|$man1dir|g
    s|@USE_SYSTEM_LIBPNG_FALSE@|$USE_SYSTEM_LIBPNG_FALSE|g
    s|@USE_SYSTEM_LIBPNG_TRUE@|$USE_SYSTEM_LIBPNG_TRUE|g
    s|@USE_SYSTEM_ZLIB_FALSE@|$USE_SYSTEM_ZLIB_FALSE|g
    s|@USE_SYSTEM_ZLIB_TRUE@|$USE_SYSTEM_ZLIB_TRUE|g
    s|@CC@|${CC-cc}|g
    s|@CFLAGS@|${CFLAGS--O}|g
    s|@CPP@|${CPP-\$(CC) -E}|g
    s|@CPPFLAGS@|${CPPFLAGS-}|g
    s|@LD@|${LD-\$(CC)}|g
    s|@LDFLAGS@|${LDFLAGS--s}|g
    s|@AR@|${AR-ar}|g
    s|@ARFLAGS@|${ARFLAGS-cru}|g
    s|@RANLIB@|${RANLIB-ranlib}|g
    s|@CP_FP@|${CP_FP-cp -f -p}|g
    s|@MKDIR_P@|${MKDIR_P-mkdir -p}|g
    s|@RM_F@|${RM_F-rm -f}|g
    s|@LIBM@|${LIBM--lm}|g
    s|@LIBPNG@|${LIBPNG--lpng}|g
    s|@LIBS@|${LIBS-}|g
    s|@LIBZ@|${LIBZ--lz}|g
    s|@LIBPNG_DISTCLEAN@|${LIBPNG_DISTCLEAN-distclean}|g
    s|@LIBPNG_DISTCLEAN_XCMD@|${LIBPNG_DISTCLEAN_XCMD-true}|g
    s|@LIBPNG_MK@|${LIBPNG_MK-Makefile}|g
    s|@LIBPNG_MK_DEF@|${LIBPNG_MK_DEF-PNGLIBCONF_H_PREBUILT=$libpng_libconf_file}|g
    s|@ZLIB_DISTCLEAN@|${ZLIB_DISTCLEAN-distclean}|g
    s|@ZLIB_MK@|${ZLIB_MK-Makefile}|g
    s|@[A-Z]*_MK@|Makefile|g
    s| *\$||
"

for subdir in \
    . \
    ./src \
    ./src/opngreduc \
    ./src/optipng \
    ./src/optipng/man \
    ./src/pngxtern
do
    test -d "$subdir" || mkdir -p "$subdir"
    sed "$sed_config" \
        "$top_srcdir/$subdir/Makefile.in" > "$subdir/Makefile"
done
