# Apply minimal permissions on all files in the given paths:
# - 755 for directories
# - 644 for files
# USAGE: set_standard_permissions $path[…]
set_standard_permissions() {
	local path
	for path in "$@"; do
		# Error out if something does not look like a path to a directory
		if [ ! -d "$path" ]; then
			return 1
		fi
		find "$path" -type d -exec chmod 755 '{}' +
		find "$path" -type f -exec chmod 644 '{}' +
	done
}

# Convert all paths in the given directory to lower case.
# USAGE: tolower $directory
tolower() {
	local directory="$1"
	if [ ! -d "$directory" ]; then
		error_not_a_directory "$directory"
		return 1
	fi

	if ! command -v 'convmv' >/dev/null 2>&1; then
		error_dependency_not_found 'convmv'
		return 1
	fi

	find "$directory" -mindepth 1 -maxdepth 1 -exec \
		convmv -f utf8 --notest --lower -r {} + >/dev/null 2>&1
}

# Convert all paths in the given directory to upper case.
# USAGE: toupper $directory
toupper() {
	local directory="$1"
	if [ ! -d "$directory" ]; then
		error_not_a_directory "$directory"
		return 1
	fi

	if ! command -v 'convmv' >/dev/null 2>&1; then
		error_dependency_not_found 'convmv'
		return 1
	fi

	find "$directory" -mindepth 1 -maxdepth 1 -exec \
		convmv -f utf8 --notest --upper -r {} + >/dev/null 2>&1
}

# Return the MIME type of a given file
# USAGE: file_type $file
# RETURNS: the MIME type, as a string
file_type() {
	local file
	file="$1"

	local file_type
	file_type=$(file --brief --dereference --mime-type "$file")

	# Everything behind the first ";" is removed,
	# so "application/x-executable; charset=binary"
	# would be returned as "application/x-executable".
	file_type=$(printf '%s' "$file_type" | cut --delimiter=';' --fields=1)

	printf '%s' "$file_type"
}
