#!/bin/sh
#
#         Name: configure (shell script)
#               part of the standard recipe
#       Author: Rick Troth, rogue programmer
#         Date: 2023-03-16 (Thu) and following
#               2023-06-28 (Wed) added sub-project support
#
# This script attempts to perform the same function as 'configure'
# as found in automake/autoconf packages, but is simpler.
#
#       Output: makefile
#               configure.sh
#               configure.h
#

cd `dirname "$0"`
#D=`pwd`
A="$*"

PREFIX=/usr

while [ ! -z "$*" ] ; do
    case "$1" in
        --prefix)
            shift ; PREFIX="$1"
            ;;
        --prefix=*|--prefi=*|--pref=*)
            PREFIX=`echo "$1" | awk -F'=' '{print $2}'`
            ;;
        --enable-static*)
            :
            ;;
        --disable-shared*)
            :
            ;;
        -*)
            echo "$0: unrecognized option '$1'"
            exit 24
            ;;
        *)
            echo "$0: unrecognized argument '$1'"
            exit 24
            ;;
    esac
    shift
done

LOCDIR=$PREFIX/share/locale                               # for xmitmsgx
#
US=`uname -s | sed 's#/##g'`
UM=`uname -m | sed 's#^i.86$#i386#' | sed 's#^armv.l$#arm#'`
case "$US" in
    Linux|FreeBSD)
        CFLAGS="$CFLAGS -fPIC"
#       LDFLAGS=
        SHFLAGS="$LDFLAGS -shared"
        if [ "$US" = FreeBSD -a ! -d "$LOCDIR" -a -d /usr/share/nls ] ; then
                                              LOCDIR=/usr/share/nls ; fi
#make a static library:
# /usr/bin/ar qc libxmitmsgx.a CMakeFiles/xmitmsgx.dir/xmitmsgx.c.o
# /usr/bin/ranlib libxmitmsgx.a
# see also: CMAKE_SHARED_LINKER_FLAGS
    ;;
    CYGWIN*)
        US="CYGWIN"
        SHFLAGS="$LDFLAGS -shared"
        ;;
    AIX)
        UM=`uname -p`
        ;;
    Sun*|Sol*)
        UM=`uname -p`
        LIBS="-lsocket -lnsl"
        ;;
    OS390)
        UM="s390x" # guessing! since both "s390x" and "s390" are viable
        CC="xlc"
        LDFLAGS="-O -qxplink"
        SHFLAGS="$LDFLAGS -qdll"
        if [ -d /usr/lib/nls/msg ] ; then LOCDIR=/usr/lib/nls/msg ; fi
        ;;
esac

#examples:
#/usr/share/locale/
#/usr/lib/nls/msg/
#/usr/share/nls/
#LOCALE=en_US.UTF-8
#LOCALE=en_US

# use the system string we just derived or override from Chicory
if [ ! -z "$CHICORY_SYSTEM" ] ; then SYSTEM=$CHICORY_SYSTEM
                                else SYSTEM="$US-$UM" ; fi

# create proper makefile from reference input
cat makefile.in \
  | sed "s#%CFLAGS%#$CFLAGS#g" \
  | sed "s#%PREFIX%#$PREFIX#g" \
  | sed "s#%SYSTEM%#$SYSTEM#g" \
  | sed "s#%LDFLAGS%#$LDFLAGS#g" \
  | sed "s#%SHFLAGS%#$SHFLAGS#g" \
  | sed "s#%LOCDIR%#$LOCDIR#g" \
  | sed 's#^        #\t#' \
  > makefile

# create configuration header file and sourcable shell script
rm -f configure.tmp
echo "CFLAGS=#$CFLAGS#"   >> configure.tmp
echo "PREFIX=#$PREFIX#"   >> configure.tmp
echo "SYSTEM=#$SYSTEM#"   >> configure.tmp
echo "LDFLAGS=#$LDFLAGS#" >> configure.tmp
echo "SHFLAGS=#$SHFLAGS#" >> configure.tmp
echo "LOCDIR=#$LOCDIR#"   >> configure.tmp                    # xmitmsgx
sed 's/#/"/g' < configure.tmp | awk '{print "#define" , $0}' | sed 's/=/ /' > configure.h
sed 's/#/"/g' < configure.tmp > configure.sh # chmod a+x configure.sh
rm configure.tmp
#define VERSION not needed
#define LOCALE not needed

# for each sub-project run the 'configure' script found there
for F in */configure ; do
    if [ -x $F -a -f $F ] ; then
        echo "+ $F $A"
        $F $A ; fi
done

exit


