#!/bin/bash

rm -rf ../dist
mkdir -p ../dist

# Remove the old app.js if it exists
rm -f ../dist/app.js

# Start the output file with the opening of the function closure
echo "(function() {" > ../dist/app.js

# Find all .js files in ../src EXCEPT index.js and append their contents
find ../src -name "*.js" -type f ! -name "index.js" | while read -r file; do
    echo "" >> ../dist/app.js  # Add a blank line between files
    echo "// Source: $file" >> ../dist/app.js  # Add a comment with the source file
    cat "$file" >> ../dist/app.js
done

# Add index.js from src folder last
if [ -f ../src/index.js ]; then
    echo "" >> ../dist/app.js
    echo "// Source: ../src/index.js" >> ../dist/app.js
    cat ../src/index.js >> ../dist/app.js
fi

# Close the function closure
echo "" >> ../dist/app.js
echo "})();" >> ../dist/app.js

echo "Build complete! Combined JavaScript files into ../dist/app.js"
