#!/bin/bash

# Copyright © 2012-2026 ScriptFanix
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# A copy of the GNU General Public License v3 is includded in the LICENSE file
# at the root of the project.

sanitizeFile() {
	shopt -s extglob
	string="$1"
	# Filenames can't contain /
	string="${string//\// }"
	if (( ${destinationfat32compat[$destination]} ))
	then
		# FAT32 forbids these characters in filenames
		string=${string//\?/ }
		string=${string//\\/ }
		string=${string//</ }
		string=${string//>/ }
		string=${string//:/ }
		string=${string//\*/ }
		string=${string//\|/ }
		string=${string//\"/ }

		# Filenames can't begin or end with ' '
		string=${string/#+( )/}
		string=${string/%+( )/}

		# Directory names can't begin or end with '.'
		if [[ $2 == dir ]]
		then
			string=${string/#+(.)/}
			string=${string/%+(.)/}
		fi

		# We can't have newlines either
		string=${string//$'\n'/ }
	fi
	echo "$string"
}
