#!/bin/bash
# Get the directory and pattern as arguments
DIR=$1
PATTERN=$2
REMOVE_ORIGINAL=$3
REMOVE_COMMENTS=$4

DASM_EXTENSION=${DASM_EXTENSION:-".dasm"}

find "$DIR" -type f -name "$PATTERN" -print0 | while IFS= read -r -d '' file; do
  # Disassemble the file and save it to a temporary file.
  OUTFILE="${file}${DASM_EXTENSION}"
  TMP=$(mktemp)
  ikdasm "$file" > "$TMP"
  if [[ "$REMOVE_COMMENTS" == "true" ]]; then
    # Remove lines starting with //
    sed -i '/^\/\//d' "$TMP"
  fi
  # Move the temporary file to the output file.
  mv "$TMP" "$OUTFILE"
  echo $(basename "$OUTFILE")

  if [[ "$REMOVE_ORIGINAL" == "true" ]]; then
    rm "$file"
  fi
done
