#!/bin/bash
# $Header: /usr/local/scalawags_cvs/Scalawags/Frc2006/scripts/startfunction,v 1.3 2006/02/20 18:34:50 abrown Exp $

# Create a new set of source files for a function foo().
# The files are created from the template files.

# Inputs:
#    template/template.c
#    template/template.h
#    template/template_t.c
#    template/template_t.in
#    template/template_t.ref
#    template/template_t.README
#    Name of the new function
#    Purpose of the function
#    Programmers Name -- Look in ${FULLNAME} and passwd file.
#    Programmers email -- Look in ${EMAILADDR}.

# Outputs:
#    foo.c
#    foo.h
#    sim/foo_t.c
#    ABSIMULATION_NOCOPROCESSOR/foo_t.in
#    ABSIMULATION_NOCOPROCESSOR/foo_t.ref


#==== INITIALIZE =================================================
progname="${0##*/}"
usage="Usage: ${progname} [-h] <funcname>"
dateformat="+%Y%m%d %H:%M:%S"
typeset -i error=0

. getstring

#==== PARSE OPTIONS ==============================================
done=no
while [ ${#} -gt 0  -a  "${done}" = "no" ]; do
  case ${1-"-?"} in
    "-?" | -h) # help
      shift
      error=-1
      ;;
    -*) # huh?
      echo -e "${progname}: Unrecognized option '${1}'." 1>&2
      shift
      error=-1
      ;;
    *) # start of parameters
      done=yes
      ;;
  esac
done
if [ $# -ne 1 ]; then
  error=-1
fi

if [ ${error} -ne 0 ]; then
  echo -e "${usage}" 1>&2
  exit ${error}
fi

# They shouldn't specify the .c at the end of the funcname, but
# in case they do, remove it.
funcname="${1%.c}"; shift

#==== SANITY CHECK ==============================================
srcdir=.
testsrcdir=sim
testtargdir=ABSIMULATION_NOCOPROCESSOR
templatedir=template
typeset -i found=1

if [ ! -d ${srcdir} ]; then
  echo "${srcdir} dir not found..." 1>&2
  found=0
fi
if [ ! -d ${testsrcdir} ]; then
  echo "${testsrcdir} dir not found..." 1>&2
  found=0
fi
if [ ! -d ${testtargdir} ]; then
  echo "${testtargdir} dir not found..." 1>&2
  found=0
fi
if [ ! -d ${templatedir} ]; then
  echo "${templatedir} dir not found..." 1>&2
  found=0
fi
if [ ${found} -ne 0 ]; then
  echo "all directories found." 1>&2
else
  echo "trying again." 1>&2
  found=1
  srcdir=../${srcdir}
  testsrcdir=../${testsrcdir}
  testtargdir=../${testtargdir}
  templatedir=../${templatedir}
  if [ ! -d ${srcdir} ]; then
    echo "${srcdir} dir not found..." 1>&2
    found=0
  fi
  if [ ! -d ${testsrcdir} ]; then
    echo "${testsrcdir} dir not found..." 1>&2
    found=0
  fi
  if [ ! -d ${testtargdir} ]; then
    echo "${testtargdir} dir not found..." 1>&2
    found=0
  fi
  if [ ! -d ${templatedir} ]; then
    echo "${templatedir} dir not found..." 1>&2
    found=0
  fi
  if [ ${found} -ne 0 ]; then
    echo "all directories found." 1>&2
  else
    echo "${progname}: Unable to find required directories.  Aborting" 1>&2
    error=-2
  fi
fi

if [ ${error} -ne 0 ]; then
  exit ${error}
fi

#------ Search for the required files.
templatec=${templatedir}/template.c
templateh=${templatedir}/template.h
templatetc=${templatedir}/template_t.c
templatetin=${templatedir}/template_t.in
templatetref=${templatedir}/template_t.ref
typeset -i found=1

if [ ! -f ${templatec} ]; then
  echo "${templatec} file not found..." 1>&2
  found=0
fi
if [ ! -f ${templateh} ]; then
  echo "${templateh} file not found..." 1>&2
  found=0
fi
if [ ! -f ${templatetc} ]; then
  echo "${templatetc} file not found..." 1>&2
  found=0
fi
if [ ! -f ${templatetin} ]; then
  echo "${templatetin} file not found..." 1>&2
  found=0
fi
if [ ! -f ${templatetref} ]; then
  echo "${templatetref} file not found..." 1>&2
  found=0
fi
if [ ${found} -eq 0 ]; then
  echo "critical files not found.  Aborting" 1>&2
  error=-3
fi

if [ ${error} -ne 0 ]; then
  exit ${error}
fi
echo "all required reference files found." 1>&2

#==== COLLECT INFO ==============================================
#------ Programmers Name and email
logname=$(id -un)
passwd=/etc/passwd
fullname="${FULLNAME-}"
if [ -z "${fullname}" ]; then
  if [ -f ${passwd} ]; then
    fullname=$(sed -ne "s/^abrown:[^:]*:[^:]*:[^:]*://p" ${passwd} \
      | sed -e "s/:.*//")
  else
    fullname=
  fi
fi
prmptlen=40
fullname="$(getstring -p ${prmptlen} "${fullname}" "Your full name?" "" \
  "^[A-Za-z]+ [A-Za-z]+" "First and Last please.")"

errormsg="Something of the form name@domain.aaa"
emailaddr="${EMAILADDR-}"
emailaddr="$(getstring -p ${prmptlen} "${emailaddr}" "Your email addr?" \
  "${errormsg}" "^[A-Za-z][-_\.A-Za-z0-9]+@[A-Za-z]+" "${errormsg}")"

#------ Purpose of the function
echo "In one line, describe the purpose of the function '${funcname}'."
explanation="$(getstring -p ${prmptlen} "" "" "You gotta say something." \
  "^[A-Za-z0-9].........." "No cheeting!  Explain it.")"

#==== PROCESS FILES ==============================================

for templfile in ${templatec} ${templateh}; do
  destfile=${srcdir}/${funcname}${templfile##${templatedir}/template}
  sed -e "s/fullname/${fullname}/" \
    -e "s/bozo@foo/${emailaddr}/" \
    -e "s/explain/${explanation}/" \
    -e "s%template%${funcname}%" \
    < ${templfile} > ${destfile}
done

for templfile in ${templatetc}; do
  destfile=${testsrcdir}/${funcname}${templfile##${templatedir}/template}
  sed -e "s/fullname/${fullname}/" \
    -e "s/bozo@foo/${emailaddr}/" \
    -e "s/explain/${explanation}/" \
    -e "s%template%${funcname}%" \
    < ${templfile} > ${destfile}
done

for templfile in ${templatetin} ${templatetref}; do
  destfile=${testtargdir}/${funcname}${templfile##${templatedir}/template}
  sed -e "s/fullname/${fullname}/" \
    -e "s/bozo@foo/${emailaddr}/" \
    -e "s/explain/${explanation}/" \
    -e "s%template%${funcname}%" \
    < ${templfile} > ${destfile}
done

#==== DONE ==============================================
exit ${error}


syntax highlighted by Code2HTML, v. 0.9.1