#!/bin/bash

#This script is designed to unpack every FreeCAD .FCStd project file into a folder in the same directory as the source file.  All files in the current directory, or any subdirectories, will be processed

#take files as arguments
files=("$@")

#no arguments provided
if [ "${files}x" == "x" ]	
then
	#recursively find all .fcstd files in the current directory
	files=$(find . -iname "*.fcstd")
fi
prev_dir="$(pwd)"
echo "${files}" | while IFS= read -r f
do
	dir=$(dirname "${f}")
	fn=$(basename "${f}")
	name="${fn%.*}"
	ext="${fn##*.}"
	#only attempt this for .FCStd files
	if [ "${ext,,}" == "fcstd" ]
	then
		#unzip into new folder in same directory as source file
		cd "${dir}"
		mkdir -p "${name}"
		unzip -qq -d "${name}" -o "${fn}"
		cd "${prev_dir}"
	fi
done
