#!/bin/sh
C=Makefile.config

# get full path
case $0 in
  /*)
    FULL=$0
    ;;
  *)
    FULL=$PWD/$0
    ;;
esac

printf "# auto-generated by $0\n" >$C
while [ $# -gt 0 ]; do
  X=$1
  shift 1
# Shell common:
  case $X in
    --help|-h|-?)
cat <<EOF
usage: $0 --prefix=<dir> var1=name1..
  --prefix=<dir>
  --target=<platform>       MacOSX,iOS,iPhoneSimulator
  --sdk=<version>
EOF
      exit 1
      ;;
# GNU Compatibility:
    --prefix=*)
      PREFIX=`printf -- "${X}" | cut -c 10-`
      ;;
    --target=*)
      TARGET=`printf -- "${X}" | cut -c 10-`
      ;;
    --sdk=*)
      SDK=`printf -- "${X}" | cut -c 7-`
      ;;
# Environment variables:
    [A-Za-z_]*=*)
      printf "${X}\n" >>$C
      k=`printf "${X}" | cut -d = -f 1`
      v=`printf "${X}" | cut -d = -f 2`
      export $k=$v
      ;;
# Others fail:
    *)
      printf "error: invalid option ${X}\n" >&2
      exit 1
      ;;
  esac
done
printf "PREFIX=${PREFIX:=/usr/local}\n" >>$C
case ${TARGET:=`uname`} in
  Linux)
    if [ -z "${CFLAGS}" ]; then
      printf "CFLAGS=-fPIC\n" >>$C
    fi
    printf "LDLIBS=-lm -ldl\n" >>$C
    ;;
  OpenBSD)
    printf "LDLIBS=-lm\n" >>$C
    ;;
  DragonFly)
    if [ -z "${CFLAGS}" ]; then
      printf "CFLAGS=-fPIC\n" >>$C
    fi
    ;;
  NetBSD)
    if [ -z "${CFLAGS}" ]; then
      printf "CFLAGS=-fPIC\n" >>$C
    fi
    printf "LDLIBS=-lm\n" >>$C
    ;;
  FreeBSD)
    if [ -z "${CFLAGS}" ]; then
      printf "CFLAGS=-fPIC\n" >>$C
    fi
    printf "LDLIBS=-lm\n" >>$C
    printf "RM=rm -f\n" >>$C
    ;;
  MacOSX|Darwin)
	# if Apple's libtool (not to be confused with GNU's) is available, which is according to libtool(1) "with -static [...] intended
	# to replace ar(5) and ranlib", use it - if it is shadowed by some install of GNU's libtool assume that a foreign environment is
	# intentionally used, and fall back to using 'ar rs' (the equivalent to 'libtool -static'), for compatibility:
    if libtool -V >/dev/null 2>/dev/null; then # Apple's libtool has -V for version, which GNU does not.
    	printf "AR=libtool\n" >>$C
    	printf "ARFLAGS=-static -o\n" >>$C
	else
    	printf "AR=ar\n" >>$C
    	printf "ARFLAGS=rs\n" >>$C
	fi
    if [ `uname -n` = 'iPhone' ]; then # building on iPhone itself, uname yields Darwin (gcc setup for current/correct arch)
      printf "CC=gcc\n" >>$C
    elif [ -z ${SDK} ]; then
      case `sw_vers -productVersion` in
        10.4.*)
          ARCHS="-arch ppc -arch i386 -arch x86_64" 
          ;;
        10.[56].*)
          ARCHS="-arch i386 -arch x86_64 -arch ppc"
          ;;
        10.6.*)
          ARCHS="-arch i386 -arch x86_64"
          ;;
        10.10.*)
          ARCHS="-arch x86_64 -arch i386"
          ;;
        10.[789].*)
          ARCHS="-arch x86_64 -arch i386"
          ;;
      esac
      printf "ASFLAGS=${ARCHS}\n" >>$C
      printf "CFLAGS=${ARCHS}\n" >>$C
      printf "CXXFLAGS=${ARCHS}\n" >>$C
    else
      SDKROOT="`xcode-select -print-path`/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${SDK}.sdk"
      printf "CFLAGS=-isysroot ${SDKROOT}\n" >>$C
      printf "CXXFLAGS=-isysroot ${SDKROOT}\n" >>$C
    fi
    ;;
  iPhoneOS|iOS)
    SDKROOT="`xcode-select -print-path`/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${SDK}.sdk"
    case "${SDK}" in
      4.3)
        ARCHS="-arch armv6"
        ;;
      6.1)
        ARCHS="-arch armv7"
        ;;
      8.1)
        ARCHS="-arch arm64"
        ;;
    esac
    printf "ASFLAGS=${ARCHS} -isysroot ${SDKROOT}\n" >>$C
    printf "CFLAGS=${ARCHS} -isysroot ${SDKROOT}\n" >>$C
    printf "CXXFLAGS=${ARCHS} -isysroot ${SDKROOT}\n" >>$C
    printf "LDFLAGS=-Wl,-syslibroot ${SDKROOT}\n" >>$C
    ;;
  iPhoneSimulator)
    SDKROOT="`xcode-select -print-path`/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator${SDK}.sdk"
    ARCHS="-arch i386"
    printf "ASFLAGS=${ARCHS} -isysroot ${SDKROOT}\n" >>$C
    printf "CFLAGS=${ARCHS} -isysroot ${SDKROOT}\n" >>$C
    printf "CXXFLAGS=${ARCHS} -isysroot ${SDKROOT}\n" >>$C
    printf "LDFLAGS=-Wl,-syslibroot ${SDKROOT}\n" >>$C
    ;;
  SunOS)
    X=`type CC`
    if [ $? -eq 0 ]; then
      printf "CXX=CC\n" >>$C
    fi
    printf "LDLIBS=-lm\n" >>$C
    ;;
  Minix)
    printf "CC=gcc\n" >>$C
    printf "CXX=g++\n">>$C
    printf "AR=ar\n"  >>$C
    ;;
  PSP)
    SDKROOT="`psp-config --pspsdk-path`"
    printf "CC=psp-gcc\n" >>$C
    printf "CXX=psp-gcc\n">>$C # don't use psp-g++, buggy
    printf "AR=psp-ar\n"  >>$C
    printf "CFLAGS=-I${SDKROOT}/include/\n" >>$C
    printf "CXXFLAGS=-I${SDKROOT}/include/\n" >>$C
    # Pulling in dyncall libs below is a hack, for some reason psp-ld is super-picky about order.
	# Use your C lib of choice, from the PSPSDK, or...
    #printf "LDLIBS=-L${SDKROOT}/lib/ -L`dirname ${FULL}`/dyncall -L`dirname ${FULL}`/dyncallback -ldyncall_s -ldyncallback_s -lm -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lstdc++ -lpsplibc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser \n" >>$C
	# ... newlib.
    printf "LDLIBS=-L${SDKROOT}/lib/ -L`dirname ${FULL}`/dyncall -L`dirname ${FULL}`/dyncallback -ldyncall_s -ldyncallback_s -lm -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lstdc++ -lc       -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser \n" >>$C
    ;;
esac

BLDTOP="$PWD"
SRCTOP=`dirname ${FULL}`

FILES=`( cd $SRCTOP ; find . -name "Makefile.generic" )`
for FILE in $FILES ; do
  SRCFILE="$SRCTOP/$FILE"
  BLDFILE="$BLDTOP/$FILE"
  SRCDIR=`dirname $SRCFILE`
  BLDDIR=`dirname $BLDFILE`
  mkdir -p "$BLDDIR"
  cat <<EOF >$BLDDIR/Makefile
VPATH = $SRCDIR
include $BLDTOP/Makefile.config
include \${VPATH}/Makefile.generic
EOF
done
