#!/bin/sh
set -euo pipefail

usage() {
	cat <<-'EOF'
		Usage:
		  plop [-m MODE] FILE
	EOF
}

MODE=
while getopts 'm:' flag; do
	case "$flag" in
		(m)
			MODE="$OPTARG"
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))


FILE="${1:-}"

eval "$(assert-arg -- "$FILE" 'FILE')"



tmp="${FILE}.plop.$$"
trap "rm -f '$tmp'" EXIT

cat > "$tmp"
if [ -n "$MODE" ]; then
	chmod "$MODE" "$tmp"
fi
mv "$tmp" "$FILE"
