#!/bin/bash

# Check if a filename is provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <filename.c|filename>"
    exit 1
fi

# Remove the .c extension if present
BASENAME="${1%.c}"
CFILENAME="$BASENAME.c"

# Check if the C file exists, otherwise initialize it
if [ ! -f "$CFILENAME" ]; then
    cat > "$CFILENAME" <<EOF
#include <stdio.h>

int main() {
	// Start

	// End
	printf("\n\nProgram execution done!\n");
	return 0;
}
EOF
    echo "Initialized $CFILENAME with a basic template."
fi

# Open in nano, compile, and run
nano "$CFILENAME" && gcc-12 -Wall "$CFILENAME" -o "$BASENAME" && ./"$BASENAME"
