#!/bin/sh
#
# Shell script for building Curry programs
#
# $Id: cymake.in,v 1.7 2004/10/25 18:40:15 berrueta Exp $
#
# Copyright (c) 2002-2003, Wolfgang Lux
# See LICENSE for the full license.
#


# Configuration parameters
cymake=`basename $0`
version=\1.0.2
build="20050329"
prefix=/usr
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
libdir=${exec_prefix}/lib
: ${CURRY_BIN=${exec_prefix}/bin}
: ${CURRY_AUX_BIN=${exec_prefix}/lib/zinc-compiler}
: ${CURRY_RTS_LIBS=${exec_prefix}/lib}
: ${CURRY_IMPORTS=${prefix}/include/libcurry-stdlib}
: ${CURRY_RTS_INCLUDE=${prefix}/include/libcurry-rts}
: ${CC="x86_64-linux-gcc "}
export CURRY_BIN CURRY_AUX_BIN CURRY_RTS_LIBS CURRY_IMPORTS CURRY_RTS_INCLUDE

# Local variables
echo=echo
exec=
verbose=
cyc=${CYC=`dirname $0`/cyc}
cymk=${CURRY_AUX_BIN}/cymk
mkopts=
cyopts=
ldopts="-L${CURRY_RTS_LIBS} "
goal=
clean=
depend=
target=
imports=
libs=
files=
temps=

# Functions
noecho ( ) { return 0; }

remove ( ) {
  for f in "$@"; do
    if test -f "$f"; then
      $echo rm -f "$f"
      $exec rm -f "$f"
    fi
  done
}

compile ( ) {
  $echo $cyc $cyopts "$@"
  $exec $cyc $cyopts "$@"
}

link ( ) {
  if test -n "$goal"; then
    $echo $cyc $cyopts $ldopts -e "$goal" -M "$target" "$@" $libs
    $exec $cyc $cyopts $ldopts -e "$goal" -M "$target" "$@" $libs
  else
    $echo $cyc $cyopts $ldopts "$@" $libs
    $exec $cyc $cyopts $ldopts "$@" $libs
  fi
}

# Option processing
while test $# -gt 0; do
  case $1 in
    # Overall options
    -M | --depend ) depend=$1;;
    -[cCS] ) echo 1>&2 "$cymake: unsupported option $1"; exit 1;;
    -a ) mkopts="$mkopts $1";;
    -e ) goal=$2; shift;;
    -e* ) goal=`expr "$1" : '-e\(.*\)'`;;
    -g | --debug ) mkopts="$mkopts $1"; cyopts="$cyopts $1";;
    --clean ) clean=--clean;;
    -o ) mkopts="$mkopts -o $2"; output="$2"; shift;;
    -o* ) mkopts="$mkopts $1"; output=`expr "$1" : '-o\(.*\)'`;;
    -n ) exec=echo;;
    -v ) verbose=-v; echo=echo cyopts="$cyopts -v";;
    -q ) echo=noecho;;

    # Include files and imported modules
    -i | -P ) cyopts="$cyopts -i$2"; imports="$imports $1 $2"; shift;;
    -i* ) cyopts="$cyopts -i`expr "$1" : '-i\(.*\)'`"; imports="$imports $1";;
    -P* ) cyopts="$cyopts -i`expr "$1" : '-P\(.*\)'`"; imports="$imports $1";;
    
    # Linker options
    -ldopts-* | --ldopts-* ) ldopts="$ldopts $1";;
    -ldopts | --ldopts ) ldopts="$ldopts $1 $2"; shift;;
    -L ) ldopts="$ldopts -L$2"; shift;;
    -L* ) ldopts="$ldopts $1";;
    -l ) libs="$libs -l$2"; shift;;
    -l* ) libs="$libs $1";;

    # Pass all other flags to the Curry compiler
    # NB some options require an argument keep this list in sync with cyc
    -[DUyIhkt] | -cc | -ccopts | --ccopts ) cyopts="$cyopts $1 $2"; shift;;
    -* ) cyopts="$cyopts $1";;

    +RTS )
	shift
	while test $# -gt 0 -a "$1" != "-RTS"; do
	  cyopts="$cyopts $1";
	  shift
	done;;

    # Save all files
    * ) files="$files $1";;
  esac
  shift
done
if test -n "$depend" &&  test -n "$clean"; then
  echo 1>&2 "$cymake: cannot specify -M and --clean together"
  exit 1
fi

# Eventually display the compiler version
test -n "$verbose" && echo 1>&2 "Zinc $cymake version $version (built on $build)"
test "$exec" && verbose= echo=noecho

# Check for input files
if test -z "$files$goal"; then
  test -n "$verbose" && exit 0
  echo 1>&2 "$cymake: no targets"
  exit 1
fi

# Generate only dependencies if requested
if test -n "$depend"; then
  test -n "$verbose" && echo 1>&2 $cymk $depend $mkopts $imports $files
  $exec $cymk $depend $mkopts $imports $files
  exit $?
fi

# Remove build script upon exit
trap 'rm -f /tmp/cymake$$' 0 1 2 3 15

# Process all targets
set -- $files
if test -n "$output" -a $# -gt 1; then
  echo 1>&2 "$cymake: cannot specify -o with multiple targets"
  exit 1
fi
if test -n "$goal" -a $# -gt 1; then
  echo 1>&2 "$cymake: cannot specify -e with multiple targets"
  exit 1
fi

if test $# -eq 0; then
  test -n "$output$" && output="-o $output"
  if test -n "$clean"; then
    remove ${output=a.out}
  else
    link $output
  fi
  exit $?
fi

for f in $files; do
  case $f in
    *.curry | *.lcurry ) target=$f;;
    *.o )
      target=`expr "$f" : '\(.*\).o'`
      if test -f "$target.lcurry"; then
	target="$target.lcurry"
      else if test -f "$target.curry"; then
        target="$target.curry"
      else
	echo 1>&2 "$cymake: missing source file for $f"; exit 1
      fi; fi
      ;;
    * )
      if test -f "$f.lcurry"; then
	target="$f.lcurry"
      else if test -f "$f.curry"; then
        target="$f.curry"
      else
	echo 1>&2 "$cymake: missing source file for $f"; exit 1
      fi; fi
      ;;
  esac
  test -n "$verbose" && echo 1>&2 $cymk $clean $mkopts $imports $f
  $cymk $clean $mkopts $imports $f > /tmp/cymake$$

  . /tmp/cymake$$
  set +e
done

# done
exit 0
