#!/bin/sh
#
# Wrapper generation script
#

CONFIGPATH=
CONFIGCOMPILEC=
CONFIGRELOC=

wrapgen_about() {
   echo "wrapgen: Wrapper generation script v1.0"
}

wrapgen_usage() {
   wrapgen_about
   echo "
Usage: wrapgen [-C <file>] [-I <path>] <source1> [<source2> ... <sourcen>]

   -C <file>  Load configuration file <file> instead of default
   -I <path>  Add <path> to includes path
   <source>   WDL source file
"
}

wrapgen_compile() {
   wrapgen_link_out=`echo $1 | sed "s/.c\$/${wrapgen_wmoext}/"`
   wrapgen_comp_out=`echo $1 | sed "s/.c\$/${wrapgen_objext}/"`
   rm -f ${wrapgen_comp_out} ${wrapgen_link_out}
   echo "Generating object: $wrapgen_comp_out"
   ${CONFIGCOMPILEC} ${wrapgen_cflags} -I ${incpath} -o ${wrapgen_comp_out} $1
   echo "   Linking object: $wrapgen_comp_out"
   eval ${CONFIGRELOC} ${wrapgen_comp_out}
   echo "   Wrapper object: $wrapgen_link_out (generated)"
}

case `uname -s` in
   CYGWIN_NT-*) wrapgen_cygwin=1
                wrapgen_objext=".obj"
                wrapgen_wmoext=".dll"
                ;;
             *) wrapgen_objext=".o"
                wrapgen_wmoext=".wmo"
                ;;
esac

wrapgen_intree=0

if [ -z "${CONFIGCOMPILEC}" -o -z "${CONFIGRELOC}" ]; then
   if [ ! -r Makefile ]; then
      echo "wrapgen: Script not properly installed"
      exit 1
   fi

   # We're probably in the tree, check for TOPDIR
   wrapgen_topdir=`grep "TOPDIR[ ]*=" Makefile | awk -F= '{ print $2 }'`
   if [ -z "$wrapgen_topdir" ]; then
      echo "wrapgen: Script not properly installed"
      exit 1
   fi

   wrapgen_intree=1
   . ${wrapgen_topdir}/configuration


   wrapgen_ld=`grep "^[ ]*LD[ ]*=" ${wrapgen_topdir}/mak/default.mak | awk -F= '{ print $2 }'`
   if [ -z "$wrapgen_ld" ]; then
      wrapgen_ld=ld
   fi

   CONFIGCOMPILEC=`grep "COMPILE.c[ ]*=" ${wrapgen_topdir}/mak/default.mak | awk -F= '{ print $2 }' | sed -e "s!.[({]CC[)}]!${compiler_path}!g" -e "s!.[({]CFLAGS[)}]!!g"`
   RELOC_LINK=`grep "RELOC_LINK[ ]*=" ${wrapgen_topdir}/wr.mak/default.mak | awk -F= '{ print $2 }' | sed -e "s!.[({]LD[)}]!${wrapgen_ld}!g"`
   RELOC_OUT=`grep "RELOC_OUT[ ]*=" ${wrapgen_topdir}/wr.mak/default.mak | awk -F= '{ print $2 }' | sed 's!.@!${wrapgen_link_out}!g'`
   CONFIGRELOC="$RELOC_LINK $RELOC_OUT"

   incpath="${wrapgen_topdir}/include"
   binpath="${wrapgen_topdir}/../bin"
   wrapgen_wrapcflags="-l -I ${wrapgen_topdir}/include -I ${wrapgen_topdir}/wss/include -I ${wrapgen_topdir}/wsi/${osdirectory}/include"
fi


# Set defaults for configuration path
if [ -z "${CONFIGPATH}" ]; then
   wrapgen_confpath=/etc/wss.conf
else
   wrapgen_confpath=$CONFIGPATH
fi

# Process command-line options
while getopts "C:hI:v" optarg $*
do
   case $optarg in
      C) wrapgen_confpath=$OPTARG
         shift
         ;;

      I) wrapgen_wrapcflags="${wrapgen_wrapcflags} -I $OPTARG"
         wrapgen_cflags="${wrapgen_cflags} -I $OPTARG"
         shift
         ;;

      v) wrapgen_about;
         exit 0
         ;;

      h) wrapgen_usage;
         exit 0
         ;;
   esac
   shift
done

if [ $# -lt 1 ]; then
   echo "wrapgen: Must specify files to generate wrapper objects from"
   exit 1
fi
wrapgen_files=$*

# Read the configuration file
if [ "$wrapgen_intree" -ne 1 ]; then
   if [ -z "$wrapgen_confpath" -o ! -r "$wrapgen_confpath" ]; then
      echo "wrapgen: Missing or invalid configuration file '$wrapgen_confpath'"
      exit 1
   fi

   wrapgen_lnum=0
   while read wrapgen_line
   do
      wrapgen_lnum=`expr ${wrapgen_lnum} + 1`
      case $wrapgen_line in
              \#*|"") ;; # skip comments and blank lines

         [a-zA-Z]*=*) wrapgen_cmd=`echo $wrapgen_line | sed 's/[ ]*=[ ]*/=/'`
                      if echo $wrapgen_cmd | egrep "^[ ]*wrapgen_" 2>/dev/null; then
                         :
                      else
                         eval $wrapgen_cmd
                      fi
                      ;;

                   *) echo "wrapgen: Invalid entry at line $wrapgen_lnum of '$wrapgen_confpath'"
                      exit 1
                      ;;
      esac
   done < "$wrapgen_confpath"

   if [ -z "$incpath" -o ! -d "$incpath" ]; then
      echo "wrapgen: Missing or invalid include path '$incpath'"
      exit 1
   fi
fi

# Process files
for wrapgen_file in $wrapgen_files
do
   case $wrapgen_file in
      *.wr) if [ ! -r $1 ]; then
               echo "wrapgen: Missing or invalid file '$1'"
               exit 1
            fi
            wrapgen_source=`echo $wrapgen_file | sed 's/.wr$/.c/'`
            rm -f $wrapgen_source
            echo "[$wrapgen_file]"
            echo "Generating source: $wrapgen_source"
            ${binpath}/wrapc $wrapgen_wrapcflags $wrapgen_file
            wrapgen_compile $wrapgen_source
            ;;

         *) echo "wrapgen: Invalid file '$wrapgen_file'"
            echo "         Files must be WDL source files (.wr)"
            exit 1
            ;;
   esac
done

exit 0

