#!/usr/local/bin/bash
#
# epix: wrapper script for ePiX
#
# Usage: epix infile[.c|.cc|.C] [outfile[.eepic]]
#
#   Script attempts to deal intelligently with missing extensions.
#   If the input file has an extension .c, .cc, or .C, then there's
#   no issue. If only the root name <infile> is given, the script
#   searches for files named infile.c, infile.cc, and infile.C in
#   that order of preference. A warning is issued for multiple matches.
#   If no output file is specified, the input file name is used. 
#
# Copyright (C) 2001--2, Andrew D. Hwang   <ahwang@mathcs.holycross.edu>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   
#

## Auto-generated file; modify as necessary ##

INSTALL_DIR=/usr/local
COMPILER=g++
PROG=epix

## Nothing below here should need modification ##

# Path to header and library
HDR_PATH=$INSTALL_DIR/include
LIB_PATH=$INSTALL_DIR/lib

# Libraries
LIBS="-lm -lepix"

INFILE=$1
declare INROOT

# get file's root name, assuming extension is one of .c, .cc, .C, or NULL
epix_fileroot() {
    if [ $1 = ${1%%".c"}.c ]
    then
	INROOT=${1%%".c"}

    elif [ $1 = ${1%%".cc"}.cc ]
    then
	INROOT=${1%%".cc"}

    elif [ $1 = ${1%%".C"}.C ]
    then
	INROOT=${1%%".C"}

    else # No recognized extension supplied
	INROOT=$1
	local infile_count
	infile_count=0

	# Look for completions; last match wins
	if [ -f $INROOT.C ]; then 
	    let infile_count=infile_count+1
	    INFILE=$INROOT.C
	fi

	if [ -f $INROOT.cc ]; then 
	    let infile_count=infile_count+1
	    INFILE=$INROOT.cc
	fi

	if [ -f $INROOT.c ]; then 
	    let infile_count=infile_count+1
	    INFILE=$INROOT.c
	fi

	if [ $infile_count -eq 0 ]
	then
	    echo "$PROG: Input file \"$INROOT\" not found" && exit 1

	elif [ $infile_count -ge 2 ]
	then
	    echo -n "$PROG: Warning -- found $infile_count completions "
	    echo "of \"$INROOT\", using $INFILE"

	# else one file found, INFILE already set
	fi

    fi
}

## Script proper starts here ##

# check number of arguments
if [ $# -gt 2 ] || [ $# -eq 0 ]
then
    echo "Usage: $PROG infile[.c|.cc|.C] [ outfile[.eepic] ]"
    echo
    exit 1;
    
elif [ $# = 2 ] 
then
    epix_fileroot $INFILE
    OUTFILE=$2
    OUTROOT=${OUTFILE%%".eepic"}
    
else # one argument
    epix_fileroot $INFILE
    OUTROOT=$INROOT
fi

# Give temporary binary a unique name
PID=$$
TEMP_BIN=$INROOT-$PID.exe

# Compile executable (temporary binary written in cwd)
echo -n "$PROG: Compiling..."
$COMPILER $INFILE -w -o $TEMP_BIN -I$HDR_PATH -I. -L$LIB_PATH  $LIBS 

if [ -f "$TEMP_BIN" ]
    then
	echo "done"
else
    echo
    echo "$PROG: Compilation failed...exiting"
    exit 1;
fi

# Write eepic file
if [ -x "$TEMP_BIN" ]
    then 
	echo -e "%% Generated from $INFILE on $(date)" > $OUTROOT.eepic
   	./$TEMP_BIN >> $OUTROOT.eepic
    echo "$PROG: Wrote $OUTROOT.eepic"

    # Clean up stale binary
    rm -f $TEMP_BIN

    echo
    exit 0;

else 
    echo "Could not create $OUTROOT.eepic...exiting"
    rm -f $TEMP_BIN

    echo
    exit 1;
fi
