#!/bin/bash

# Directory to search for files
DIR=$1

# Log file
LOG_FILE="replacement_log.txt"

# Function to replace '--force-yes' with '--allow-change-held-packages'
replace() {
	find "$DIR" -type f -name "*.sh" -exec grep -l -- '--force-yes' {} \; | while read -r file
do
	sed -i 's/--force-yes/--allow-change-held-packages/g' "$file"
	echo "$file" >> "$LOG_FILE"
done
}

# Function to reverse the replacement
undo() {
	if [ -e $LOG_FILE ]; then
		while read file
		do
			sed -i 's/--allow-change-held-packages/--force-yes/g' "$file"
		done < $LOG_FILE
		rm $LOG_FILE
	else
		echo "No log file found."
	fi
}

# Check the second argument
if [ "$2" == "undo" ]; then
	undo
else
	replace
	cat "$LOG_FILE"
fi
