#!/bin/sh
DATE="15/01/2001"
MYCOMMAND=/usr/local/share/vecfem3/vempfade
#**************************************************************************
#**                                                                     ***
#**   arguments:                                                        ***
#**              $1 => workpiece name                                   ***
#**              $2 => other problem directory                          ***
#**   input file:  $1.resource => data set of the vecfem parameters     ***
#**                $1.equation => data set of the functional equation   ***
#**   output file:  $1.f => the source code                             ***
#**                                                                     ***
#**************************************************************************
#**                                                                     ***
#**        Copyrights University of Karlsruhe, 1996                     ***
#**        Program by L. Grosz                                          ***
#**                                                                     ***
#**************************************************************************
#**                                                                     ***
if [ $# -lt 1 ]
 then
   echo "illegal number of arguments:" >&2
   echo "call:  vembuild <workpiece>" >&2
   exit 1
fi
echo "VECFEM: Code Generator"
echo "Version: $DATE"
echo "Copyrights by University of Karlsruhe 1996"
#
#** finde the vecfem variables
#
if [ "$MYCOMMAND" = "" ] ; then
  . vempfade
  echo vempfade from `dirname $(which vempfade)`
else
  VEMPATH=`dirname $MYCOMMAND`
  SC=`basename $MYCOMMAND`
  if test -r $VEMPATH/vempfade ; then
     . $VEMPATH/vempfade
     echo vempfade from $VEMPATH
  else
    HELP=`which $SC`
    VEMPATH=`dirname $HELP`
    if test -r $VEMPATH/vempfade ; then
      . $VEMPATH/vempfade
      echo vempfade from $VEMPATH
    else
      . vempfade
      echo vempfade from `dirname $(which vempfade)`
    fi
  fi
fi
#
#---------------------------------
EXT=$$
WORKPIECE=$1

test -r $WORKPIECE.equation
if [ $? -eq 1 ]
  then
  echo ">> input file "$WORKPIECE".equation was not found !" >&2
  echo ">> abend !" >&2
  exit 1
fi

rm -f $WORKPIECE.f
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#** smoothing of resource file : result file is resource.$EXT       ***
#**                                                                 ***
#**  the chaining of the dummy.resource file will ensure that       ***
#**  all parameters gets a value.                                   ***
#**  the name of the parameters are changed to capital letters      ***
#**                                                                 ***
cat -s $WORKPIECE.resource $VECFEM_ROOT/share/vecfem3/dummy.resource |
sed -e 's/#.*//
        s/\//\\\//g
        s/	//g
        s/ //g
         /=$/d
         /^$/d' |

$VECFEM_AWK 'BEGIN{FS="="
           OUT="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	   IN="abcdefghijklmnopqrstuvwxyz"}{
if (1 < NF) {
  if (1 < NR) {printf "\n"}
  BUFFER=$1
  k=1
  while (k<=length(OUT)) {
         gsub(substr(IN,k,1),substr(OUT,k,1),BUFFER)
         k++}
  printf "%s=%s",BUFFER,$2 }
else { printf "%s",$1 }}
END{}' > /tmp/resource.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
echo "default values for parameters are added."
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  get the important parameters:                                  ***
#**                                                                 ***
#**  OUTPUT_NSEL gives the number of sets selected for              ***
#**  solution output                                                ***
#**                                                                 ***
MESH_PREP=`$VECFEM_AWK \
     'BEGIN{FS="="} {if ($1=="MESH_PREP") {print $2 ;exit}}' /tmp/resource.$EXT`

MESH_POSTP=`$VECFEM_AWK \
     'BEGIN{FS="="} {if ($1=="MESH_POSTP") {print $2 ;exit}}' /tmp/resource.$EXT`

NK=`$VECFEM_AWK 'BEGIN{FS="="} {if ($1=="NK") {print $2 ;exit}}' /tmp/resource.$EXT`
NK2=`echo $NK | sed 's/[^0-9]//g'`

if [ "$NK" != "$NK2" ]
  then
    echo ">> error : illegal NK = "$NK >&2
    rm -f /tmp/*.$EXT 
    exit 1
fi

Y=$NK
if [ $Y -lt 1 ]
  then
    echo ">> error : illegal NK = "$NK >&2
    rm -f /tmp/*.$EXT 
    exit 1
fi

OUTPUT_NSEL=`$VECFEM_AWK 'BEGIN{nsel=1; FS="="} {if ($1=="OUTPUT_INDEX" ) { \
               nsel=int((length($2)+'$NK'-1)/'$NK'); \
	       if(nsel==0) {nsel=1}; exit}} ;\
	       END{print nsel}' /tmp/resource.$EXT`

OUTPUT_ERRELEM=`$VECFEM_AWK \
     'BEGIN{FS="="} {if ($1=="OUTPUT_ERRELEM") {print $2+0 ;exit}}' /tmp/resource.$EXT`

SOLVER_INTERP=`$VECFEM_AWK \
     'BEGIN{FS="="} {if ($1=="SOLVER_INTERP") {print $2+0 ;exit}}' /tmp/resource.$EXT`

SOLVER_STEADY=`$VECFEM_AWK \
     'BEGIN{FS="="} {if ($1=="SOLVER_STEADY") {print $2+0 ;exit}}' /tmp/resource.$EXT`

Y=`echo $SOLVER_STEADY | sed 's/[^0-9]//g'`
if [ $Y -le 0  ]; then
    echo "nonsteady problem solver is used !" 
    SOLVER_STEADY=0
    VECFEM4=$VECFEM_ROOT/bin/vecfem4p.h
    VECFEML=$VECFEM_ROOT/bin/vecfem1p.h
  else
    echo "steady problem solver is used !"
    SOLVER_STEADY=1
    VECFEM4=$VECFEM_ROOT/bin/vecfem4e.h
    VECFEML=$VECFEM_ROOT/bin/vecfem1e.h
fi
echo "solution components NK="$NK
echo "sets of selected solution components OUTPUT_NSEL="$OUTPUT_NSEL
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  check selection of MESH_PREP and                               ***
#**  select source code for mesh read :                             ***
#**                                                                 ***
case $MESH_PREP in
   1 | 001 | print | vecfem ) MESH_PREP="print";
                 echo "mesh is read from a vecfem input file." ;
                 MESHIN=$VECFEM_ROOT/share/vecfem3/printin.h ;;
   2 | 002 | i-deas ) MESH_PREP="i-deas";
                  echo "the preprocessor is i-deas.";
                  MESHIN=$VECFEM_ROOT/share/vecfem3/ideasin.h ;;
   3 | 003 | patran ) MESH_PREP="patran";
                  echo "the preprocessor is patran.";
                  MESHIN=$VECFEM_ROOT/share/vecfem3/patranin.h ;;
  * ) echo "illegal preprocessor "$MESH_PREP"." >&2;
      echo "please contact the vecfem programmers to implement" >&2;
      echo "no interface to your preprocssor !" >&2 ;
      rm -f /tmp/*.$EXT ;
      exit 1 ;;
esac
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  check selection of MESH_POSTP :                                ***
#**  some postprocessors allows no element-by-element output        ***
#**  of error indicator !                                           ***
#**                                                                 ***
case $MESH_POSTP in
   1 | 001 | print ) MESH_POSTP="print";
                 echo "solution and errors are printed.";
                 echo "only error on nodes is allowed." ;
                 OUTPUT_ERRELEM="0" ;;
   2 | 002 | i-deas ) MESH_POSTP="i-deas";
                  echo "the postprocessor is i-deas." ;;
   3 | 003 | patran ) MESH_POSTP="patran";
                  echo "the postprocessor is patran." ;;
   4 | 004 | isvas  ) MESH_POSTP="isvas";
                  echo "the postprocessor is isvas." ;
                  echo "only error on nodes is allowed." ;
                  OUTPUT_ERRELEM="0" ;;
   5 | 005 | ensight ) MESH_POSTP="ensight";
                  echo "the postprocessor is ensight." ;
                  echo "only error on nodes is allowed." ;
                  OUTPUT_ERRELEM="0" ;;
   6 | 006 | gnuplot ) MESH_POSTP="gnuplot";
                  echo "the postprocessor is gnuplot." ;
                  echo "only error on nodes is allowed." ;
                  OUTPUT_ERRELEM="0" ;;
   7 | 007 | dataexplorer ) MESH_POSTP="dataexplorer";
                  echo "the postprocessor is explorer." ;
                  echo "only error on nodes is allowed." ;
                  SOLVER_INTERP="1" ;
                  OUTPUT_ERRELEM="0" ;;
   8 | 008 | avs-ucd ) MESH_POSTP="avs-ucd";
                  echo "the postprocessor is avs/ucd." ;
                  echo "only error on nodes is allowed." ;
                  SOLVER_INTERP="1" ;
                  OUTPUT_ERRELEM="0" ;;
    *) echo "illegal postprocessor "$MESH_POSTP"." >&2;
       rm -f /tmp/*.$EXT ;
       exit 1;;
esac
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  select source code for mesh and solution output:               ***
#**                                                                 ***
case $MESH_POSTP in
 i-deas) POSTOUT1=$VECFEM_ROOT/share/vecfem3/ideasout1.h;
         POSTOUT2=$VECFEM_ROOT/share/vecfem3/ideasout2.h;
         POSTOUT4=$VECFEM_ROOT/share/vecfem3/openout.h;
         if [ "$OUTPUT_ERRELEM" = "1" ]
           then
             POSTOUT3=$VECFEM_ROOT/share/vecfem3/ideasout31.h
             VECFEM7=$VECFEM_ROOT/bin/vecfem71.h;
           else
             POSTOUT3=$VECFEM_ROOT/share/vecfem3/ideasout32.h
             VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;
           fi;;
 patran) POSTOUT1=$VECFEM_ROOT/share/vecfem3/patranout1.h;
         POSTOUT2=$VECFEM_ROOT/share/vecfem3/patranout2.h;
         POSTOUT4=$VECFEM_ROOT/share/vecfem3/noopen.h;
         if [ "$OUTPUT_ERRELEM" = "1" ]
           then
             POSTOUT3=$VECFEM_ROOT/share/vecfem3/patranout31.h
             VECFEM7=$VECFEM_ROOT/bin/vecfem71.h;
           else
	     POSTOUT3=$VECFEM_ROOT/share/vecfem3/patranout32.h
             VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;
          fi;;

  print) POSTOUT1=$VECFEM_ROOT/share/vecfem3/printout1.h;
         POSTOUT2=$VECFEM_ROOT/share/vecfem3/printout2.h;
         POSTOUT3=$VECFEM_ROOT/share/vecfem3/printout32.h;
         POSTOUT4=$VECFEM_ROOT/share/vecfem3/openout.h;
         VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;;

  isvas) POSTOUT1=$VECFEM_ROOT/share/vecfem3/isvasout1.h;
         POSTOUT2=$VECFEM_ROOT/share/vecfem3/isvasout2.h;
         POSTOUT3=$VECFEM_ROOT/share/vecfem3/isvasout32.h;
         POSTOUT4=$VECFEM_ROOT/share/vecfem3/openout.h;
         VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;;

  ensight) POSTOUT1=$VECFEM_ROOT/share/vecfem3/ensightout1.h;
           POSTOUT2=$VECFEM_ROOT/share/vecfem3/ensightout2.h;
           POSTOUT3=$VECFEM_ROOT/share/vecfem3/ensightout32.h;
           POSTOUT4=$VECFEM_ROOT/share/vecfem3/noopen.h;
           VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;;
 gnuplot) POSTOUT1=$VECFEM_ROOT/share/vecfem3/gnuplotout1.h;
          POSTOUT2=$VECFEM_ROOT/share/vecfem3/gnuplotout2.h;
          POSTOUT4=$VECFEM_ROOT/share/vecfem3/openout.h;
          POSTOUT3=$VECFEM_ROOT/share/vecfem3/gnuplotout32.h
          VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;;

 dataexplorer) POSTOUT1=$VECFEM_ROOT/share/vecfem3/dxout1.h;
               POSTOUT2=$VECFEM_ROOT/share/vecfem3/dxout2.h;
               POSTOUT4=$VECFEM_ROOT/share/vecfem3/openout.h;
               POSTOUT3=$VECFEM_ROOT/share/vecfem3/dxout32.h;
               VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;;

 avs-ucd) POSTOUT1=$VECFEM_ROOT/share/vecfem3/avsout1.h;
               POSTOUT2=$VECFEM_ROOT/share/vecfem3/avsout2.h;
               POSTOUT4=$VECFEM_ROOT/share/vecfem3/noopen.h;
               POSTOUT3=$VECFEM_ROOT/share/vecfem3/avsout32.h;
               VECFEM7=$VECFEM_ROOT/bin/vecfem72.h;;
esac
if [ "$MESH_PREP" = "$MESH_POSTP" ]
  then
    POSTOUT1=$VECFEM_ROOT/share/vecfem3/nomeshout.h
fi
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  the script to set the parameters into the vecfem code.         ***
#**  the solution file names and titles are writen to an include    ***
#**  file.                                                          ***
#**                                                                 ***
#**  generated file: /tmp/include.$EXT                              ***
#**                  /tmp/sedin.$EXT                                ***
#**                                                                 ***
touch /tmp/include.$EXT
echo "s/%OUTPUT_NSEL_/$OUTPUT_NSEL/1" > /tmp/sedin.$EXT
echo "s/;/'/g" >> /tmp/sedin.$EXT
echo "/^#/d" >> /tmp/sedin.$EXT
echo "s/%WORKPIECE_/$WORKPIECE/g" >> /tmp/sedin.$EXT
echo "s/%SOLVER_INTERP_/$SOLVER_INTERP/g" >> /tmp/sedin.$EXT

$VECFEM_AWK 'BEGIN{FS="="; nsel='$OUTPUT_NSEL' ; nk='$NK' ; outputtitle=0 ; outputfile=0
	       outputerrindex=0 ; meshreduce=0 ; outputindex=0 }{

if ($1 == "OUTPUT_ERRINDEX" && outputerrindex==0 ) {
value="" ; k=1; outputerrindex=1
  while (k<=nk){d=0
    if (k<=length($2)){ if (substr($2,k,1)!="0"){d=1}}
    value=sprintf("%s,%s",value,d);k++}
  value=substr(value,2,2*nk-1)
printf "s/%%%s_/%s/1\n",$1,value >> "/tmp/sedin.'$EXT'"}

else { if ($1 == "MESH_REDUCE" && meshreduce==0) {
value="" ; k=1; meshreduce=1
  while (k<=nk){d=2
    if (k<=length($2)){ if (substr($2,k,1)=="1"){d=1}}
    value=sprintf("%s,%s",value,d);k++}
  value=substr(value,2,2*nk-1)
printf "s/%%%s_/%s/1\n",$1,value >> "/tmp/sedin.'$EXT'"}

else {if ($1 == "OUTPUT_INDEX" && outputindex==0) {
value="" ; i=1; outputindex=1
  while (i<=nsel){k=1; help=""
    while (k<=nk){d=0
      if (k+nk*(i-1)<=length($2)){if (substr($2,k+nk*(i-1),1)!="0"){d=1}}
      help=help "," d;k++}
  value=value help;i++}
value=substr(value,2,2*nk*nsel-1)
printf "s/%%%s_/%s/1\n",$1,value >> "/tmp/sedin.'$EXT'"}

else{ if (($1 == "OUTPUT_TITLE" && outputtitle==0) ||
          ($1 == "OUTPUT_FILE"  && outputfile==0)) {
      if ($1 == "OUTPUT_TITLE") {
          outputtitle=1; varname="TITLE"}
       else {
          outputfile=1 ; varname="FILE"}
      fsel=split($2,PARTS,",") ; k=1
      while (k<=nsel){
        if (k<=fsel) {
          printf "      %s(%s)=;%s;\n",varname,k,PARTS[k] >> "/tmp/include.'$EXT'"}
        else {
          printf "      %s(%s)=;solution.file%s;\n",varname,k,k >> "/tmp/include.'$EXT'"}
        k++}}
else { if (($2=="") || ($1=="")) {  
       printf ">> illegal value in resource file line %s\n  %s\n",NR,$0
      exit 1 }
    else {
      printf "s/%%%s_/%s/1\n",$1,$2 >> "/tmp/sedin.'$EXT'" }} }}}}
' /tmp/resource.$EXT >&2

if [ $? != 0 ]
 then
   echo ">> abend !" >&2
   rm -f /tmp/*.$EXT 
   exit 1
fi
echo "resource file was analysed." 
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  now the equation file is handled                               ***
#**  generated files : /tmp/terms.$EXT                              ***
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#** smoothing                                                       ***
#**      remove commend lines                                       ***
#**      eleminate spaces and avoid lines                           ***
#**      capital to small letters                                   ***
#**      yank statements on more than one line                      ***
#**                                                                 ***
sed -e 's/#.*//
        y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
        s/ //g
        s/	//g
        /^$/d' $WORKPIECE.equation |

$VECFEM_AWK 'BEGIN{FUNCTIONAL=0}{
if ((match($0,"point")!=0) ||  (match($0,"line")!=0) ||
    (match($0,"area")!=0) || (match($0,"volume")!=0)    ) {
    if ((NR!=1) && (FUNCTIONAL==0)) {printf "\n"}
    FUNCTIONAL=1}

if ((FUNCTIONAL==0) && (match($0,"=")!=0) && (NR!=1))
   {printf "\n%s",$0} else {printf "%s",$0}}
END{printf "\n"}'  |
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#** separate equation-file                                          ***
#**       copy declaration <name>=<expr>                            ***
#**       Dirichlet conditions: 'u<i>=<expr>' to 'DB<i>=<expr>'     ***
#**       Initial conditions: 'u0<i>=<expr>' to 'U0<i>=<expr>'      ***
#**       'volume{<expr>}' to 'IM(3)=<expr>'                        ***
#**       'area{<expr>}' to 'IM(2)=<expr>'                          ***
#**       'line{<expr>}' to 'IM(1)=<expr>'                          ***
#**       'point{<expr>}' to 'IM(0)=<expr>'                         ***
#**                                                                 ***
#**   expr=(x<i>,tau<ij>,u<i>,u<i>x<j>,ut<i>,ut<i>x<j>,             ***
#**                                    v<i>,v<i>x<j>,h(i))          ***
#**         fortran expressions but '^' instead of '**'             ***
#**                                                                 ***
$VECFEM_AWK 'BEGIN{FS="="}{
p=match($0,"point")
l=match($0,"line")
a=match($0,"area")
v=match($0,"volume")
#
# while no integration kernels were found the declartions and the dirichlet
# conditions are fetched:
#
if ( (p==0) && (l==0) && (a==0) && (v==0) ) {
  if (""==$2)
	{printf ">> error in equation file statement %s\n",NR
	      printf "  %s\n",$0
	      exit 99}
  if (match($1,"u[0-9]+")==0) {
       printf "%s=%s\n",$1,$2 >> "/tmp/terms1.'$EXT'" }
  else { if (match($1,"u0[0-9]+")==0) 
     {printf "DB(%s)=%s\n",substr($1,2),$2 >> "/tmp/terms1.'$EXT'" } else
     {printf "U0(%s)=%s\n",substr($1,2),$2 >> "/tmp/terms1.'$EXT'" }}}
else {
  
  if (v>0) {
    end=match(substr($0,v),"\}")
    start=match(substr($0,v),"\{")
    if ((end==0) || (start==0) || (end <= start)) {
        print ">> error in volume of functional equation !"
	printf "  %s\n",$0
        exit 99}
    printf "IM(3)=%s\n",substr($0,v+start,end-start-1) >> "/tmp/terms1.'$EXT'" }
  if (a>0) {
    start=match(substr($0,a),"\{")
    end=match(substr($0,a),"\}")
    if ((end==0) || (start==0) || (end <= start)) {
        print ">> error in area of functional equation !"
	printf "  %s\n",$0
        exit 99}
    printf "IM(2)=%s\n",substr($0,a+start,end-start-1) >> "/tmp/terms1.'$EXT'" }
  if (l>0) {
    start=match(substr($0,l),"\{")
    end=match(substr($0,l),"\}")
    if ((end==0) || (start==0) || (end <= start)) {
        print ">> error in line of functional equation !"
	printf "  %s\n",$0
        exit 99}
    printf "IM(1)=%s\n",substr($0,l+start,end-start-1) >> "/tmp/terms1.'$EXT'" }
  if (p>0) {
    start=match(substr($0,p),"\{")
    end=match(substr($0,p),"\}")
    if ((end==0) || (start==0) || (end <= start)) {
        print ">> error in point of functional equation !"
	printf "  %s\n",$0
        exit 99}
    printf "IM(0)=%s\n",substr($0,p+start,end-start-1) >> "/tmp/terms1.'$EXT'" }

    if (NF==1) {condition=0}
    else { if (substr($2,1,3)=="min")
                          {condition=1} else {condition=0}}
    printf "FMINIMIZE=%s\n",condition >> "/tmp/terms1.'$EXT'"

   }}' >&2

if [ $? != 0 ]
 then
   echo ">> abend !" >&2
   rm -f /tmp/*.$EXT
   exit 1
fi
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#** substitute tau                                                  ***
#**       tau-variable on line integration gets index 1             ***
#**       tau-derivative on line integration gets index 11          ***
#**       tau-derivative is replaced by derivative                  ***
#**                                                                 ***
sed -e 's/[0-9]tau/&x/g
        s/taux1/x1/g
        s/taux2/x2/g
        s/taux/x1/g
        s/tau1/tau11/g
        s/tau2/tau21/g
        s/tau111/tau11/g
        s/tau211/tau21/g
        s/tau112/tau12/g
        s/tau212/tau22/g' /tmp/terms1.$EXT > /tmp/terms.$EXT
#**                                                                 ***
#** end of the awk-script to reformat the equations                 ***
#**                                                                 ***
echo "FSTEADY="$SOLVER_STEADY >> /tmp/terms.$EXT
echo "equation file was analysed."
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  know the algebra program $1 is called                          ***
#**  generated files : /tmp/vemuserb.$EXT                           ***
#**                    /tmp/vemuserf.$EXT                           ***
#**                    /tmp/vemusrfu.$EXT                           ***
#**                    /tmp/vemusrfut.$EXT                          ***
#**                    /tmp/vemusermasks.$EXT                       ***
#**  the vecfem variables are elongated so that in the fortran      ***
#**  code of the user routines the code will not become longer      ***
#**                                                                 ***
case $VECFEM_ALGEBRA in
 maple) $VECFEM_ROOT/bin/vemmaple $NK $VECFEM_ROOT/share/vecfem3$EXT;;
 axiom) $VECFEM_ROOT/bin/vemaxiom $NK $VECFEM_ROOT/share/vecfem3$EXT;;
 maxima) $VECFEM_ROOT/bin/vemmaxima $NK $VECFEM_ROOT/share/vecfem3$EXT;;
 *) echo 'unknown algebra program '$VECFEM_ALGEBRA; >&2
    rm -f /tmp/*.$EXT ;
    exit 1;;
esac
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**   if there is no output the algebra program failed !            ***
#**                                                                 ***
LINES=`cat -s /tmp/vemuserf.$EXT /tmp/vemusrfu.$EXT /tmp/vemusrfut.$EXT | wc -l`
if [ $LINES = 0 ]
  then
   echo ">> "$VECFEM_ALGEBRA" failed !" >&2
   rm -f /tmp/*.$EXT
   exit 1
fi
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  assemlage of /tmp/vemuserb.$EXT into /tmp/user.$EXT            ***
#**                                                                 ***
cat $VECFEM_ROOT/share/vecfem3/userb1.h > /tmp/user.$EXT

cat -s /tmp/vemuserb.$EXT |
$VECFEM_AWK '{line=substr($0,7)
      gsub(" ","",line)
      if (substr($0,6,1)!=" ") printf"%s",line
      else printf"\n      %s",line}
      END{printf"\n"}' |

$VECFEM_AWK 'BEGIN{FS="="; BUFFER="";GRA[1]=0}
    {while (substr($1,7,1)!="B")
        {BUFFER=BUFFER $0 "\n"
         next}
     start=index($1,"(")
     end=index($1,")")
     split(substr($1,start+1,end-start-1),ARG,",")
     if (GRA[1]!=ARG[1])
       {if (GRA[1]!=0)
          {printf" %s    CONTINUE\n", wert
           printf"      ENDIF\n\n"}
      wert=ARG[1]
      if (wert!=0)
        {printf"      IF (COMPU.EQ.%s) THEN\n",ARG[1]
         printf"      DO %s Z=1,NDC\n",wert
         print BUFFER $0 "\n"
         BUFFER=""
	 GRA[1]=ARG[1]}}
     else
       {print BUFFER $0 "\n"
        BUFFER=""}}
     END{if (GRA[1]!=0)
          {printf" %s    CONTINUE\n", wert
           printf"      ENDIF\n\n"}}
     ' >> /tmp/user.$EXT

  if [ $? != 0 ]
    then
     echo ">> abend !" >&2
     rm -f /tmp/*.$EXT 
     exit 1
  fi
  cat $VECFEM_ROOT/share/vecfem3/userb3.h >> /tmp/user.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  assemlage of /tmp/vemuseru0.$EXT into /tmp/user.$EXT           ***
#**                                                                 ***
cat $VECFEM_ROOT/share/vecfem3/useru01.h >> /tmp/user.$EXT

cat -s /tmp/vemuseru0.$EXT |
$VECFEM_AWK '{line=substr($0,7)
      gsub(" ","",line)
      if (substr($0,6,1)!=" ") printf"%s",line
      else printf"\n      %s",line}
     END{printf"\n"}'      |

$VECFEM_AWK 'BEGIN{FS="="; BUFFER="";GRA[1]=0}
    {while (substr($1,7,2)!="U0")
        {BUFFER=BUFFER $0 "\n"
         next}
     start=index($1,"(")
     end=index($1,")")
     split(substr($1,start+1,end-start-1),ARG,",")
     if (GRA[1]!=ARG[1])
       {if (GRA[1]!=0)
          {printf" %s    CONTINUE\n", wert
           printf"      ENDIF\n\n"}
      wert=ARG[1]
      if (wert!=0)
        {printf"      IF (COMPU.EQ.%s) THEN\n",ARG[1]
         printf"      DO %s Z=1,NE\n",wert
         print  BUFFER $0 "\n"
         BUFFER=""
	 GRA[1]=ARG[1]}}
     else
       {print BUFFER $0 "\n"
        BUFFER=""}}
     END{if (GRA[1]!=0)
          {printf" %s    CONTINUE\n", wert
           printf"      ENDIF\n\n"}}
     ' >> /tmp/user.$EXT

  if [ $? != 0 ]
    then
     echo ">> abend !" >&2
     rm -f /tmp/*.$EXT 
     exit 1
  fi
  cat $VECFEM_ROOT/share/vecfem3/useru03.h >> /tmp/user.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  assemlage of /tmp/vemuserf.$EXT append to /tmp/user.$EXT       ***
#**                                                                 ***
cat $VECFEM_ROOT/share/vecfem3/userf1.h >> /tmp/user.$EXT

cat -s /tmp/vemuserf.$EXT |
$VECFEM_AWK '{line=substr($0,7)
      gsub(" ","",line)
      if (substr($0,6,1)!=" ") printf"%s",line
      else printf"\n      %s",line}
     END{printf"\n"}' |

$VECFEM_AWK 'BEGIN{FS="="; BUFFER="";GRA[1]=0; GRA[2]=0}
    {while (substr($1,7,1)!="F")
      {BUFFER=BUFFER $0 "\n"
       next}
     start=index($1,"(")
     end=index($1,")")
     split(substr($1,start+1,end-start-1),ARG,",")
     if (GRA[1]!=ARG[1]||GRA[2]!=ARG[2])
        {if (GRA[1]!=0)
           {printf" %s    CONTINUE\n", wert
            printf"      ENDIF\n\n"}
         wert=4*(ARG[1]-1)+(ARG[2]+1)
         if (wert!=0)
	   {printf"      IF ((COMPV.EQ.%s).AND.(CLASS.EQ.%s)) THEN\n",ARG[1],ARG[2]
 	    printf"      DO %s Z=1,NELIS\n",wert
	    print BUFFER $0 "\n"
            BUFFER=""
	    GRA[1]=ARG[1]
	    GRA[2]=ARG[2]
	    printf"      MASKF(%s,%s)=.TRUE.\n",ARG[1],ARG[2] >> "/tmp/vemusermasks.'$EXT'"}}
       else
	  {print BUFFER $0 "\n"
	  BUFFER=""}}
     END{if (GRA[1]!=0)
         {printf" %s    CONTINUE\n", wert
          printf"      ENDIF\n\n"}}
     '   >> /tmp/user.$EXT

  if [ $? != 0 ]
    then
     echo ">> abend !" >&2
     rm -f /tmp/*.$EXT
     exit 1
  fi
  cat $VECFEM_ROOT/share/vecfem3/userf3.h >> /tmp/user.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  assemlage of /tmp/vemusrfu.$EXT append to /tmp/user.$EXT       ***
#**                                                                 ***
cat $VECFEM_ROOT/share/vecfem3/usrfu1.h >> /tmp/user.$EXT

cat -s /tmp/vemusrfu.$EXT |
$VECFEM_AWK '{line=substr($0,7)
      gsub(" ","",line)
      if (substr($0,6,1)!=" ") printf"%s",line
      else printf"\n      %s",line}
      END{printf"\n"}'  |

$VECFEM_AWK 'BEGIN{FS="="; BUFFER=""; GRA[1]=0; GRA[2]=0; GRA[3]=0}
    {while (substr($1,7,1)!="F")
	 {BUFFER=BUFFER $0 "\n"
	  next}
     start=index($1,"(")
     end=index($1,")")
     split(substr($1,start+1,end-start-1),ARG,",")
     if (GRA[1]!=ARG[1]||GRA[2]!=ARG[2]||GRA[3]!=ARG[3])
        {if (GRA[1]!=0)
            {printf" %s    CONTINUE\n", wert
             printf"      ENDIF\n\n"}
         wert='$NK'*4*(ARG[1]-1)+4*(ARG[2]-1)+ARG[3]+1
         if (wert!=0)
            {printf"      IF ((COMPV.EQ.%s).AND.(COMPU.EQ.%s).AND.(CLASS.EQ.%s)) THEN\n",ARG[1],ARG[2],ARG[3]
             printf"      DO %s Z=1,NELIS\n",wert
             print BUFFER $0 "\n"
             BUFFER=""
             GRA[1]=ARG[1]
             GRA[2]=ARG[2]
             GRA[3]=ARG[3]
             printf"      MASKL(%s,%s,%s)=.TRUE.\n",ARG[1],ARG[2],ARG[3] >> "/tmp/vemusermasks.'$EXT'"}}
     else
        {print BUFFER $0 "\n"
         BUFFER=""}}
     END{if (GRA[2]!=0)
            {printf" %s    CONTINUE\n", wert
             printf"      ENDIF\n\n"}}
     '  >> /tmp/user.$EXT 	

  if [ $? != 0 ]
    then
     echo ">> abend !" >&2
     rm -f /tmp/*.$EXT 
     exit 1
  fi

  cat $VECFEM_ROOT/share/vecfem3/usrfu3.h >> /tmp/user.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**  assemlage of /tmp/vemusrfut.$EXT append to /tmp/user.$EXT      ***
#**                                                                 ***
cat $VECFEM_ROOT/share/vecfem3/usrfut1.h >> /tmp/user.$EXT

cat -s /tmp/vemusrfut.$EXT |
$VECFEM_AWK '{line=substr($0,7)
      gsub(" ","",line)
      if (substr($0,6,1)!=" ") printf"%s",line
      else printf"\n      %s",line}
     END{printf"\n"}' |

$VECFEM_AWK 'BEGIN{FS="="; BUFFER=""; GRA[1]=0; GRA[2]=0; GRA[3]=0}
    {while (substr($1,7,1)!="F")
	 {BUFFER=BUFFER $0 "\n"
	  next}
     start=index($1,"(")
     end=index($1,")")
     split(substr($1,start+1,end-start-1),ARG,",")
     if (GRA[1]!=ARG[1]||GRA[2]!=ARG[2]||GRA[3]!=ARG[3])
        {if (GRA[1]!=0)
            {printf" %s    CONTINUE\n", wert
             printf"      ENDIF\n\n"}
         wert='$NK'*4*(ARG[1]-1)+4*(ARG[2]-1)+ARG[3]+1
         if (wert!=0)
            {printf"      IF ((COMPV.EQ.%s).AND.(COMPU.EQ.%s).AND.(CLASS.EQ.%s)) THEN\n",ARG[1],ARG[2],ARG[3]
             printf"      DO %s Z=1,NELIS\n",wert
             print BUFFER $0 "\n"
             BUFFER=""
             GRA[1]=ARG[1]
             GRA[2]=ARG[2]
             GRA[3]=ARG[3]
             printf"      MASKK(%s,%s,%s)=.TRUE.\n",ARG[1],ARG[2],ARG[3] >> "/tmp/vemusermasks.'$EXT'"}}
     else
        {print BUFFER $0 "\n"
         BUFFER=""}}
     END{if (GRA[2]!=0)
            {printf" %s    CONTINUE\n", wert
             printf"      ENDIF\n\n"}}
     '  >> /tmp/user.$EXT 	

  if [ $? != 0 ]
    then
     echo ">> abend !" >&2
     rm -f /tmp/*.$EXT 
     exit 1
  fi

cat $VECFEM_ROOT/share/vecfem3/usrfut3.h >> /tmp/user.$EXT
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#** correct the vecfem variables :                                  ***
#**                                                                 ***
  sed 's/ = /=/g
 /^[^C]/y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
       s/PREVALUE/RVDPRM(Z,1)/g
       s/XZZZZ1/X(Z,1)/g
       s/XZZZZ2/X(Z,2)/g
       s/XZZZZ3/X(Z,3)/g
       s/TAUZZZZZ11/TAU(Z,1,1)/g
       s/TAUZZZZZ12/TAU(Z,1,2)/g
       s/TAUZZZZZ21/TAU(Z,2,1)/g
       s/TAUZZZZZ22/TAU(Z,2,2)/g
       s/TAUZZZZZ31/TAU(Z,3,1)/g
       s/TAUZZZZZ32/TAU(Z,3,2)/g
       s/DUDXZZ(/DUDX(Z,/g
       s/UZZ(/U(Z,/g
       s/U0ZZ(.)/U0(Z)/g
       s/DUTDXZZ(/DUTDX(Z,/g
       s/UTZZ(/UT(Z,/g
       s/B(.)/B(Z)/g
       s/F0(.,./F0(Z/g
       s/F1(.,.,/F1(Z,/g
       s/F0U(.,.,./F0U(Z/g
       s/F0UX(.,.,./F0UX(Z/g
       s/F1U(.,.,./F1U(Z/g
       s/F1UX(.,.,./F1UX(Z/g
       s/F0UT(.,.,./F0UT(Z/g
       s/F0UTX(.,.,./F0UTX(Z/g
       s/F1UT(.,.,./F1UT(Z/g
       s/F1UTX(.,.,./F1UTX(Z/g' /tmp/user.$EXT >>/tmp/user2.$EXT
#**                                                                 ***
#**  user routines are ready now                                    ***
#**                                                                 ***
#**********************************************************************
#**                                                                 ***
#**   all files are gathered                                        ***
#**                                                                 ***
cat $VECFEM_ROOT/bin/vecfem1.h \
    $VECFEML           \
    $VECFEM_ROOT/bin/vecfem1a.h \
    /tmp/vemusermasks.$EXT    \
    /tmp/include.$EXT  \
    $MESHIN            \
    $VECFEM_ROOT/bin/vecfem2.h \
    $POSTOUT1          \
    $POSTOUT4          \
    $VECFEM_ROOT/bin/vecfem3.h \
    $VECFEM4           \
    $VECFEM_ROOT/bin/vecfem5.h \
    $POSTOUT2          \
    $VECFEM_ROOT/bin/vecfem6.h \
    $VECFEM7           \
    $POSTOUT3          \
    $VECFEM_ROOT/bin/vecfem8.h \
    $VECFEM_ROOT/bin/vecfem9.h \
    $VECFEM_ROOT/bin/vecfem10.h \
    /tmp/user2.$EXT |
#**                                                                 ***
#**   insert resource parameters                                    ***
#**                                                                 ***
sed -f /tmp/sedin.$EXT |
#**                                                                 ***
#**   fit lines according to FORTRAN standart length                ***
#**                                                                 ***
$VECFEM_AWK '
    BEGIN{laenge=72}
    length<=laenge {printf"%s\n",$0}
    length> laenge {line=$0
 	    printf"%s\n",substr(line,1,laenge)
 	    line=substr(line,laenge+1)
 	    while (length(line)>0)
 	      {printf"     #%s\n",substr(line,1,laenge-6)
 	       line=substr(line,laenge-5)}} ' > $WORKPIECE.f


  if [ $? != 0 ]
    then
     echo ">> abend !" >&2
     exit 1
    else
     echo "vecfem program "$WORKPIECE".f is created." 
  fi
#**********************************************************************
#**                                                                 ***
#**   all work files are deleted                                    ***
#**                                                                 ***
#**********************************************************************
rm -f /tmp/*.$EXT 
