currentStep=15
bellaSteps=30
angle=90

# camera array holding mat4
#==========================
camMat4=(-0.991192 0 0 0 0 0 -1 0 0 0.991192 -0.13243 0 0 -155.109 29.1869 1)

# Note all bash vars are global, except local
#============================================
function func_radians () {
    radians_result=$(awk -v degrees=$1  \
        'BEGIN{ 
                PI=3.14159265
                result = degrees/180*PI; 
                printf "%f", result}'
    )
}

function func_multiply () {
    func_multiply_return=$(awk -v X=$1 -v Y=$2 \
        'BEGIN{ 
                result = X*Y; 
                printf "%f", result}'
    )
}

function func_add () {
    func_add_return=$(awk -v X=$1 -v Y=$2 \
        'BEGIN{ 
                result = X+Y; 
                printf "%f", result}'
    )
}

# expect frames starting at 1
function func_lerp () {
	func_lerp_result=$(awk -v "startVal=$1" -v "endVal=$2" -v "currentFrame=$3" -v "totalFrames=$4" \
    'BEGIN {print ((endVal-(startVal))*((currentFrame-1)/(totalFrames-1)))+(startVal)}')
}

function anim_angle_to_mat4() {
    func_anim_angle_to_mat4_return=$(awk -v angle=$1 -v currentStep=$2  -v totalSteps=$3 \
        'BEGIN{ 
                PI=3.14159265
                elemA = cos(((angle/totalSteps)*(currentStep-1))/180*PI); 
                elemB = sin(((angle/totalSteps)*(currentStep-1))/180*PI); 
                elemC = sin(((angle/totalSteps)*(currentStep-1))/180*PI)*-1; 
                elemD = cos(((angle/totalSteps)*(currentStep-1))/180*PI); 
                printf "%f %f 0 0 %f %f 0 0 0 0 1 0 0 0 0 1", elemA, elemB, elemC, elemD }'
        ) 
}

# convert angle to mat4
#anim_angle_to_mat4 $angle 15 30
#rotationMat4=(${func_anim_angle_to_mat4_return})
#echo ${#rotMat4[@]}

# bash 3.2 on MacOS does not support associative arrays

# using map array since we lack associative arrays
func_dot_product_mat4 () {
    # convert angle to mat4
    anim_angle_to_mat4 $1 $2 $3
    local rotationMat4=(${func_anim_angle_to_mat4_return})
    resultingMat4=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
    local linearIndexMap=(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
    local skipIndexMap=(0 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15)

    local firstOffset=0
    local secondOffset=0
    local fourCount=0
    # walk every row, every column, do dot product math
    for (( i = 0 ; i < 16 ; i++ )); do
        for ((colCount = 0 ; colCount < 4 ; colCount++ )); do
            #echo "${i} ${linearIndexMap[$((colCount+firstOffset))]} ${skipIndexMap[$((colCount+secondOffset))]}"
            camIndex=${linearIndexMap[$((colCount+firstOffset))]} 
            rotationIndex=${skipIndexMap[$((colCount+secondOffset))]}
            func_multiply ${camMat4[ $camIndex ]} ${rotationMat4[ $rotationIndex ]}
            func_add ${resultingMat4[$i]} $func_multiply_return
            resultingMat4[$i]=$func_add_return
        done
        secondOffset=$((secondOffset+4))
        fourCount=$((fourCount+1))
        if [ $fourCount == 4 ]; then
            fourCount=0
            firstOffset=$((firstOffset+4))
            secondOffset=0
        fi
    done
}


for ((frameCount = 1 ; frameCount <= 30 ; frameCount++ )); do
    func_lerp 0 90 $frameCount 30
    echo -e "\n=======\n$func_lerp_result"
    echo "func_dot_product_mat4 ${func_lerp_result} ${frameCount} 30"
    func_dot_product_mat4 ${func_lerp_result} ${frameCount} 30
    echo "mat4( ${resultingMat4[@]} );"
done

func_dot_product_mat4 90 30 30
echo "mat4( ${resultingMat4[@]} );"
