#!/bin/sh
#
#         Name: configure (shell script)
#               part of the standard recipe
#               set-up the source directory for [re]making
#       Author: Rick Troth, Houston, Texas, USA
#         Date: 1997-Feb-02
#       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
#

#
# run from the resident directory
cd `dirname "$0"`
Z=`basename $0`
A="$*"

#
# set a default installation prefix
PREFIX=/usr/local
#PREFIX=/usr

#
# parse arguments and options
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

#
# establish build defaults
CFLAGS=""
#CFLAGS="-DOECS"
LDFLAGS=""
LIBS=""

LOCDIR=$PREFIX/share/locale                               # for xmitmsgx

#
# identify this environment/system
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)              # OS/390, z/OS
        UM="s390x" # guessing! since both "s390x" and "s390" are viable
        CC="xlc"
        CFLAGS="-DOECS -D_OE_SOCKETS -D__UU"
#       LDFLAGS="-O -qxplink"
        LDFLAGS=""
        SHFLAGS="$LDFLAGS -qdll"
        if [ -d /usr/lib/nls/msg ] ; then LOCDIR=/usr/lib/nls/msg ; fi
        ;;
    zVM|VMESA)          # VM/ESA, z/VM
        UM="s390" # guessing! but CMS is historically slow w/r/t 64-bit
        CC="xlc"
        CFLAGS="-DOECS -D_OE_SOCKETS"
        LIBS="-l//posxsock -l//vmmtlib"
        ;;
esac

#
# determine a locale
if [ -z "$LOCALE" -a ! -z "$LANG" ] ; then LOCALE="$LANG" ; fi
#LOCALE 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

#
# announce
echo "$Z: setting up for $SYSTEM ..."

#
# silently move old makefile aside:
mv makefile makefile.old 2>/dev/null

#
# start a fresh "makefile" and stamp it:
echo "# this file generated by '$0'" > makefile
RC=$? ; if [ $RC -ne 0 ] ; then exit $RC ; fi
date +'# %Y-%b-%d %T %Z %a' >> makefile
RC=$? ; if [ $RC -ne 0 ] ; then exit $RC ; 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#%LIBS%#$LIBS#g" \
  | sed "s#%LOCDIR%#$LOCDIR#g" \
  | sed "s#%LOCALE%#$LOCALE#g" \
  | sed 's#^        #\t#' \
  >> makefile
RC=$? ; if [ $RC -ne 0 ] ; then exit $RC ; fi

#
# 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
echo "LOCALE=#$LOCALE#"   >> 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 here
#define LOCALE not needed here

#
# 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

echo "$Z: done."

exit


