#!/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 create a skeleton for an new openscad project
# every project is created in its own directory
# usage : scadcreate projectname [print_part1 print_part2 mod1 mod2 ...]
# every module that start with "print_" can generate automatically an stl file with scad2stl
# the skeleton program is build as follows:
# 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
#
# 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
# 
#
# dependancies: scad2include
# 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
###################################

###################################
# start  definitions
STANDARDLIBS="include <mhlib/mhlib.scad>;";	# standard library to include
PROJECTS="/home/matthieu/openscad_drawings";	# project working directory (should already exists)
AUTHOR="Matthieu Hendriks";			# replace with your name
LICENSE="t.b.d";				# any license on your drawing
#
# end of definitions
###################################
if [ "$1" == "" ]; then 
	echo "Create SCAD project <projectname> [modulename1 modulename2 ...]"
	exit 1
fi
date=$(date '+%Y-%m-%d')
project=$1;	# for easy reading
echo ${PROJECTS};
cd "${PROJECTS}";

if [ -d "$project" ]; then
	echo "Project already exists"
	exit 1
fi
mkdir $project
read -r -d '' template <<-EOFHDR
//******************
// Title: $project  
// Author: $AUTHOR
// Date: $date
// License: $LICENSE

//******************
// start include section
//******************
$STANDARDLIBS

// *****************
// start constants section
// *****************
// constants
	\$fn=128;
	run="main";

// *****************
// start run section
// *****************
/* runstart Start:autogenerated run file */

/* runeind End:autogenerated run file */

// *****************
// start module section
// *****************

EOFHDR

echo "$template" > $project/$project.scad
all="";
allmain="";
x=10;
first="";
shift
while(($#)) ; do
	mname=$1;
	if [ "$mname" = "shape" ]; then
		printf '\nmodule %s() {\n\tpoints=[[0,0],[0,1],[1,1],[1,0],[0,0]];\n\tpolygon(points);}\n' $mname >> $project/$project.scad
	else 
		printf '\nmodule %s() {\n}\n' $mname >> $project/$project.scad
	fi
	shift
done
cd $project
scad4include $project

