#!/bin/sh

HEAD=""
TAIL=""

output_head() {
  echo "$HEAD"
}

output_tail() {
  echo "$TAIL"
}

if_1() {
  S1_IF=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\1/' -e 's/[ ][ ]*$//'`
  V1_T=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\3/' -e 's/[ ][ ]*$//'`
  V1_F=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\5/' -e 's/[ ][ ]*$//'`
  V1_A="$2"
  echo "if ( $S1_IF ) {"
  output_head
  if [ "`echo $V1_T`" != "" ]; then
    echo "$V1_T" | sed -e 's/[ ][ ]*$//' -e 's/[;]/;/g' -e 's/[{]/{/g' -e 's/$//' | tr '\a' '\n' | sed -e 's/^[ ][ ]*//'
  fi
  if [ "`echo $V1_A`" != "" ]; then
    echo "$V1_A" | sed -e 's/[ ][ ]*$//' -e 's/[;]/;/g' -e 's/[{]/{/g' -e 's/$//' | tr '\a' '\n' | sed -e 's/^[ ][ ]*//'
  fi
  output_tail
  echo "}"
  echo "else {"
  output_head
  if [ "`echo $V1_F`" != "" ]; then
    echo "$V1_F" | sed -e 's/[ ][ ]*$//' -e 's/[;]/;/g' -e 's/[{]/{/g' -e 's/$//' | tr '\a' '\n' | sed -e 's/^[ ][ ]*//'
  fi
  if [ "`echo $V1_A`" != "" ]; then
    echo "$V1_A" | sed -e 's/[ ][ ]*$//' -e 's/[;]/;/g' -e 's/[{]/{/g' -e 's/$//' | tr '\a' '\n' | sed -e 's/^[ ][ ]*//'
  fi
  output_tail
  echo "}"
}

if_2() {
  S2_IF=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\1/' -e 's/[ ][ ]*$//'`
  V2_T=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\3/' -e 's/[ ][ ]*$//'`
  V2_F=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\5/' -e 's/[ ][ ]*$//'`
  echo "if ( $S2_IF ) {"
  if_1 "$2" "$V2_T $3"
  echo "}"
  echo "else {"
  if_1 "$2" "$V2_F $3"
  echo "}"
}

if_3() {
  S3_IF=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\1/' -e 's/[ ][ ]*$//'`
  V3_T=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\3/' -e 's/[ ][ ]*$//'`
  V3_F=`echo "$1" | sed -e 's/\([^@]*\)\([@][ ]*\)\([^@]*\)\([@][ ]*\)\([^@]*\)/\5/' -e 's/[ ][ ]*$//'`
  echo "if ( $S3_IF ) {"
  if_2 "$2" "$3" "$V3_T"
  echo "}"
  echo "else {"
  if_2 "$2" "$3" "$V3_F"
  echo "}"
}

################################################################

# 
# test
#

echo "/******* TEST2 *******/"
echo ""
HEAD="HEAD"
TAIL="TAIL"
if_2 "IF_A @ DO_A_true; @ DO_A_false;"  "IF_B @ DO_B_true; @ DO_B_false;"
echo ""

echo "/******* TEST3 *******/"
echo ""
HEAD="HEAD"
TAIL="TAIL"
if_3 "IF_A @ DO_A_true; @ DO_A_false;"  "IF_B @ DO_B_true; @ DO_B_false;"  "IF_C @ DO_C_true; @ DO_C_false;" 
echo ""

################################################################

#
# define HEAD and TAIL of loop
#

HEAD="`cat <<EOF
    for ( i=0 ; i < n ; i++ ) {
        val = vals[i];
        if ( isfinite(val) ) {
            res_sum += val;
            vv = val * val;
            res_sum2 += vv;
EOF`"
TAIL="`cat <<EOF
            cnt ++;
        }
    }
EOF`"


cat <<EOF
/******* This code is generated by create_loops.sh *******/
static long stat_pixels_sum_minmax( double vals[],long n, long ii,long jj,long kk,
                                    const fits_image *this_p, void *_p )
{
    struct stat_pixels_results *resp = (struct stat_pixels_results *)_p;
    double res_sum = resp->sum;
    double res_sum2 = resp->sum2;
    double res_sum3 = resp->sum3;
    double res_sum4 = resp->sum4;
    double res_min = resp->min;
    double res_max = resp->max;
    double *res_img_buf = resp->img_buf;
    size_t res_img_buf_cnt = resp->img_buf_cnt;
    long i, cnt;
    double val, vv;
    cnt = 0;
EOF

SRC1_IF="isfinite(res_min) && isfinite(res_max)"
SRC1_T="if ( val < res_min ) res_min = val; if ( res_max < val ) res_max = val;"
SRC1_F="if ( isfinite(res_min) ) { if ( val < res_min ) res_min = val; } else res_min = val; if ( isfinite(res_max) ) { if ( res_max < val ) res_max = val; } else res_max = val;"

SRC2_IF="res_img_buf != NULL"
SRC2_T="res_img_buf[res_img_buf_cnt] = val; res_img_buf_cnt ++;"
SRC2_F=""

SRC3_IF="resp->do_sum3 == true"
SRC3_T="res_sum3 += vv * val; res_sum4 += vv * vv;"
SRC3_F=""

if_3 "$SRC1_IF @ $SRC1_T @ $SRC1_F" \
     "$SRC2_IF @ $SRC2_T @ $SRC2_F" \
     "$SRC3_IF @ $SRC3_T @ $SRC3_F"

cat <<EOF

    resp->sum = res_sum;
    resp->sum2 = res_sum2;
    resp->sum3 = res_sum3;
    resp->sum4 = res_sum4;
    resp->min = res_min;
    resp->max = res_max;
    resp->img_buf_cnt = res_img_buf_cnt;

    return cnt;
}
/******* End of generated code *******/

EOF


cat <<EOF
/******* This code is generated by create_loops.sh *******/
static long stat_pixels_sum( double vals[],long n, long ii,long jj,long kk,
			     const fits_image *this_p, void *_p )
{
    struct stat_pixels_results *resp = (struct stat_pixels_results *)_p;
    double res_sum = resp->sum;
    double res_sum2 = resp->sum2;
    double res_sum3 = resp->sum3;
    double res_sum4 = resp->sum4;
    double *res_img_buf = resp->img_buf;
    size_t res_img_buf_cnt = resp->img_buf_cnt;
    long i, cnt;
    double val, vv;
    cnt = 0;
EOF

SRC1_IF="res_img_buf != NULL"
SRC1_T="res_img_buf[res_img_buf_cnt] = val; res_img_buf_cnt ++;"
SRC1_F=""

SRC2_IF="resp->do_sum3 == true"
SRC2_T="res_sum3 += vv * val; res_sum4 += vv * vv;"
SRC2_F=""

if_2 "$SRC1_IF @ $SRC1_T @ $SRC1_F" \
     "$SRC2_IF @ $SRC2_T @ $SRC2_F"

cat <<EOF

    resp->sum = res_sum;
    resp->sum2 = res_sum2;
    resp->sum3 = res_sum3;
    resp->sum4 = res_sum4;
    resp->img_buf_cnt = res_img_buf_cnt;

    return cnt;
}
/******* End of generated code *******/

EOF

