#!/bin/bash

# --- export_freecad.sh ---
# Imports an OpenSCAD file into FreeCAD and saves it as a .FCStd project.
#
# !! REQUIRES FREECAD & OPENSCAD WORKBENCH !!
# This script depends on FreeCAD being installed, 'FreeCADCmd.exe'
# being in your system's PATH, and the OpenSCAD workbench being installed
# within FreeCAD.
#
# Usage: ./export_freecad.sh <source.scad> [output.FCStd]
#
# Arguments:
#   $1: source_file  - Input .scad file (Required)
#   $2: output_file  - Output .FCStd file (Optional)

# --- Input Validation ---
if [ -z "$1" ]; then
    echo "Usage: $0 <source.scad> [output.FCStd]"
    echo "Error: Source file not specified."
    exit 1
fi

# --- Argument Parsing ---
SOURCE_FILE="$1"
OUTPUT_FILE="$2"

# --- Set Default Output Filename ---
if [ -z "$OUTPUT_FILE" ]; then
    OUTPUT_FILE="${SOURCE_FILE%.scad}.FCStd"
fi

# --- Step 1: Import .scad and save as .FCStd using FreeCAD ---
echo "Importing '$SOURCE_FILE' and creating '$OUTPUT_FILE' using FreeCAD..."
# Note: Assuming FreeCADCmd.exe for Windows. Use 'freecadcmd' on Linux.
FreeCADCmd.exe -c save_fcstd.py "$SOURCE_FILE" "$OUTPUT_FILE"

if [ $? -ne 0 ]; then
    echo "Error: FreeCAD project creation failed. Is FreeCAD and the OpenSCAD workbench installed and in your PATH?"
    exit 1
fi

# --- Completion Message ---
echo "Export complete: '$OUTPUT_FILE' created successfully." 