
function always_mx ()
{
  retval=-1
  case $1 in
    m)   retval=0 ;;        # matrix
    cm)  retval=0 ;;        # complex matrix
    fm)  retval=0 ;;        # float matrix
    fcm) retval=0 ;;        # float complex matrix
    bm)  retval=0 ;;        # boolean matrix

    dm)  retval=1 ;;        # diagonal matrix
    cdm) retval=1 ;;        # complex diagonal matrix
    fdm) retval=1 ;;        # float diagonal matrix
    fcdm)retval=1 ;;        # float complex diagonal matrix
    sm)  retval=1 ;;        # sparse matrix
    scm) retval=1 ;;        # sparse complex matrix
    sbm) retval=1 ;;        # sparse boolean matrix
    pm)  retval=1 ;;        # permutation matrix

    b)   retval=2 ;;        # bool
    s)   retval=2 ;;        # scalar
    cs)  retval=2 ;;        # complex scalar
    fs)  retval=2 ;;        # float scalar
    fcs) retval=2 ;;        # float complex scalar

    cell)     retval=2 ;;
    dms)      retval=2 ;;
    str)      retval=2 ;;
    double)   retval=2 ;;
    float)    retval=2 ;;
    int)      retval=2 ;;
    i8)       retval=2 ;;
    i16)      retval=2 ;;
    i32)      retval=2 ;;
    i64)      retval=2 ;;
    ui8)      retval=2 ;;
    ui16)     retval=2 ;;
    ui32)     retval=2 ;;
    ui64)     retval=2 ;;
    conv)     retval=2 ;;
    concat)   retval=2 ;;
    template) retval=2 ;;

    *) echo "I don't know what " $1 " is."; exit ;;
  esac
}

ls *.cc | tr '.-' '  ' | while read dummy1 op1 op2 dummy2; do
  if [ "$dummy2" = "" ]; then
    continue
  fi

  always_mx $op1; a=$retval;
  always_mx $op2; b=$retval;

  if [ $a$b = 00 ]; then
    CHECK=CHECK2D
  elif [ $a$b = 01 ]; then
    CHECK=CHECK2D_1
  elif [ $a$b = 10 ]; then
    CHECK=CHECK2D_2
  else
    CHECK=""
  fi
  if [ "$CHECK" != "" ]; then
    new=op-$op1-$op2.cc 
    old=$new.old
    mv $new $old
    echo $CHECK $new
    awk -f add_check.awk -v CHECK=$CHECK < $old > $new || exit
  else
    echo "Skipping op-$op1-$op2.cc"
  fi
done

for new in `ls op-dm-template.cc`; do
  old=$new.old
  mv $new $old
  awk -f add_check.awk -v CHECK=VALIDATE < $old > $new || exit
done
