#!/bin/bash

#file_location=~/Coding/WIP/
file_location=~/Coding/simutrans-pak128.britain/trains

case "$1" in

    -L) echo "locomotive option passed" 
	type="locomotives"
	;; # Message for -l option

    -R) echo "Railcar (Multiple Unit) option passed"
        type="railcars"
	;; # Message for -r option

    -C) echo "Carriage option passed"
        type="carriages"
	;; # Message for -c option

    -W) echo "Wagon option passed"
	type="wagons"
	;; # Message for -w option

     *) echo "$1 not recognized. Script usage: -[L locomotive| R railcar| C carriage| W wagon] file"
	exit 38
     	;; # In case you typed a different option other than a,b,c
 
esac
#shift <- in theory, but we won't operate without declaring the destination ($2 can stay $2).

echo "Replacing image locations to $type for all versions of $2"
echo "The following .dat files have been edited:"

#Replace the text within the suitable dat files found. -path...-o is to exclude the subdirectories
find $file_location -type f -path "$file_location/*/*" -prune -o -name "$2*" -print -execdir sed -i -E -e 's/\/images/\/'"$type"'/' {} +

#Move the images to their new locations. The finding process is identical to avoid errors
find $file_location/images -type f -name "$2*" -exec mv -t $file_location/$type {} +


