#!/usr/bin/env bash
# upmove -- moves files from a given directory to one level above

. lib.bash || exit

shopt -s dotglob

for dir; do
	debug "processing arg ${dir@Q}"
	if [[ ! -d "$dir" ]]; then
		err "item '$dir' is not a directory"
		continue
	fi
	if [[ "$dir" -ef . ]]; then
		err "item '$dir' is the current directory"
		continue
	fi
	dir=${dir%/}

	for fileA in "$dir"/*; do
		fileB=${fileA#"$dir/"}
		debug "checking file ${fileA@Q} <--> ${fileB@Q}"
		if [[ "$fileB" == . || "$fileB" == .. ]]; then
			:
		elif [[ -e "$fileB" ]]; then
			if cmp -s "$fileA" "$fileB"; then
				vmsg "identical files would conflict: '$fileB'"
			else
				err "file would conflict: '$fileB'"
			fi
		fi
	done

	(( !errors )) || continue

	find "$dir" -mindepth 1 -maxdepth 1 \
		-not -name "$dir" \
		-print -exec mv -t "$PWD" {} +

	rmdir -v "$dir"
done

((!errors))
