#!/bin/bash
###################################
# author: Matthieu Hendriks
# date: 01-2024
# license: free to use and adept, no warranty grom the author
# title: scadcreate
# description:
# simple bash script to regenerate the run section from scadcreate
# this script replaces section 4 as created in scadcreate
# if you add "print_" modules you can recreate and replace section 4 
# first step is copy the original file to "bck_projectname.bck_scad"
#
# with the scad4include:
# a standard main module is created with commented out all the "print_"  modules, simply uncomment to debug per module
# a standard run_all module with a call to all the "print_"  modules shifted along the x-as
#
# this script should be run within the project directory
#
# scadcreate:
# Section 1: title /author/ metadata
# Section 2: include standard libraries		# adapt in this script
# Section 3: global variables definitions
# Section 4: run section do not change this is can be regenerated with scad4include
# section 5: module secion # here starts the fun, alle give modules name are created as an empty module to fill in later
#
# 
# installation:
#	sudo apt install yad			# yet another dialogprogramm for file selection and a popup menu
#	sudo cp scad2stl /usr/bin		# included script
#	sudo cp scad2mp4 /usr/bin		# included script
#	sudo cp scad4include /usr/bin		# included script
#	sudo cp scadcreate /usr/bin		# included script
#	sudo cp osm /usr/bin			# included script
###################################


# create a backup file to bck_projectname.bck_scad
string=$1;			# $1 contains project name or openscadfile
project=${string%.*};		# skip extension
x=10;				# move along x for run_all
runfile="run_$project.scad";
begin="/* runstart Start:autogenerated run file */";
einde="/* runeind End:autogenerated run file */";
echo "$begin" > $runfile
shopt -s lastpipe		# necessary for keeping all 
grep -e "^module print_" -e "^module main" -e "^module run_all"  $project.scad | awk '{print $2}' | awk -F '(' '{print $1}' | while read -r line ; do
echo $line;
echo "if (run == \"$line\") { $line(); } " >> $runfile;
if [ "$line" != "run_all" ] && [ "$line" != "main" ]; then
	all="${all}$(printf '\n// %s();\n' $line)"
	allmain="${allmain}$(printf '\n\tmvx(%d) %s();' $x $line)"
	x=$((x+10))	
fi
done

echo -e "\nmodule main() {\n$all\n}" >> $runfile;
echo -e "\nmodule run_all() {\n$allmain\n}" >> $runfile;
echo "$einde" >> $runfile
echo "Done";


echo -n > tempfile.tmp;
begin="/* runstart";
einde="/* runeind";
skip="0";
while IFS= read -r line
do

if [[ "$line" == "$begin"* ]]; then 
	skip="1";
fi
if [  "$skip" ==  "1" ] && [[  "$line" ==  "$einde"* ]]; then
	skip="2";
fi 
if [  "$skip" ==  "0" ] || [ "$skip" == "3" ]; then 
	echo "$line" >>tempfile.tmp;
fi

if [  "$skip" ==  "2" ]; then
	cat $runfile >>tempfile.tmp;
	skip=3;
fi
#	echo "$skip:: $line" 
done < "$project.scad"
if [  "$skip" ==  "0" ]; then
	cat $runfile >>tempfile.tmp;
fi
mv $project.scad bck_$project.bck_scad 
mv tempfile.tmp $project.scad
rm $runfile
