#!/bin/bash
# test script to apply filters to all files in a directory
# author: Rostislav Lán

# activate virtual environment when using this script
# this is necessary to run the script using the correct python and libraries versions
source .venv/bin/activate

#----------------------------Configuration------------------------------#

# place all image files to one folder
input_path=res/test/Jenetrics/leva

# !!!!!!!!!!!!!!!!!!
# this is very important, run this on directories containing files with the same dpi
dpi=500
# !!!!!!!!!!!!!!!!!!

# recommend png, it's supported by opencv
format=tif

# name of configuration file
config_file=conf/conf.json

# name of preset in conf. file
presets=("ridge")

# generate stl files and set generation mode {"p" for planar, "c for curved, "m" for mapped}
generate_stl=true
generate_stl_mode="c"

# in 1/10 of milimeters
height_line=2.2
height_base=12

curv_x=1.3
curv_y=1.8

iteration=1
finger_x=0
finger_y=0
finger_z=0
finger_model="res/finger_backup/finger-mod.stl"


#----------------------------Application---------------------------#

# function to apply filter to all files in directory
apply_filter() {
    for in in ${file_arr[@]}
    do
        # check that the file exists
        [[ -f "$in" ]] || continue

        # skip stl files and files with preset name in them
        [[ "$in" == *"ridge"* ]] && continue
        [[ "$in" == *".stl" ]] && continue

        ((i++))
        echo -e "\n|----------------------- File no. $i: $in ------------------------------|\n"
        if $generate_stl; then

            out="${in%%${match1}*}${match1}$format"

            if [[ "$in" == "$out" ]]; then
                echo "SCRIPT: Changing filename to avoid overwrite"
                out="${in%%${match1}*}_mod${match1}$format"
            fi
            case "$generate_stl_mode" in
                "p")
                    python3 $exec_path $in $out $dpi -c $config_file $1 --stl p $height_line $height_base || break
                    ;;
                "c")
                    python3 $exec_path $in $out $dpi -c $config_file $1 --stl c $height_line $height_base $curv_x $curv_y || break
                    ;;
                "m")
                    python3 $exec_path $in $out $dpi -c $config_file $1 --stl m $height_line $iteration $finger_x $finger_y $finger_z $finger_model|| break
                    ;;
                *)
                    echo "SCRIPT: Invalid stl generation mode"
                    exit 1
                    ;;
            esac
        else
            out="${in%%${match1}*}_$preset${match1}$format"
            python3 $exec_path $in $out $dpi -c $config_file $1 || break
        fi 
    done
}

# for pattern matching in filenames
exec_path=src/main.py
match1=.
match2=/
i=0
j=0
file_arr=()

# get all filenames in all subdirectories
for file in $input_path/*
do
    file_arr=("${file_arr[@]}" "$file")
done

# apply all given filter presets to all the discovered files
for preset in ${presets[@]}
do
    echo -e "\n|----------------------- Filter "$preset" ------------------------------|\n"
    j=0
    apply_filter $preset $j
done

echo -e "\n|--------------------------- Done ----------------------------------|\n"
echo "SCRIPT: Skipped $j files"
echo "SCRIPT: Generated $i files"
