#!/bin/bash
#===============================================================================
#
# cg_clover.sh - generates clover source files 
#
#-------------------------------------------------------------------------------
#
# Copyright (C) 2016 Hinnerk Stueben
#
# This file is part of BQCD -- Berlin Quantum ChromoDynamics program
#
# BQCD is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BQCD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
#
#------------------------------------------------------------------------------

set -u
input=$1
output=$2
filename=$output
extension=${output##*.}

    case $filename in
        *_.*) underscore=_;;
        *)    underscore=;;
    esac

    ifs=$IFS
    IFS=_
    set -- ${filename%.*}
    IFS=$ifs

    prefix=$1; shift

    #case $1 in
    #    c2) input=clover_mult_a2 ;;
    #    co) input=clover_mult_ao ;; 
    #esac
    shift

    case $1 in
        block) block=1; shift ;;
        *)     block=0
    esac

    clover_type=$1; shift
    spincol_type=$1; shift

    case $1 in
        r4)  rkind=4; simd= ;;
        r8)  rkind=8; simd= ;;
        r4s) rkind=4; simd=_simd ;;
        r8s) rkind=8; simd=_simd ;;
    esac

{
    echo "#define CLOVER_TYPE $clover_type"
    echo "#define SPINCOL_TYPE $spincol_type"
    echo "#define PRECISION_R$rkind"
    if ((block)); then
    echo "#define BLOCK"
    fi
    cat $input
} > $output

exit
