#!/bin/bash
# vx:	vi wrapper to create executable files if they don't exist
for A; do
	if [ ! -s "$A" ]; then
		touch "$A"
	fi
	chmod +x "$A"
	if [ -s "$A" ]; then
		continue
	fi
	name="$(basename "$A")"
	header_comment="# [arg ...]"
	header_comment_slashes="// $name:	"
	header_comment_python="\"\"\" $name:	\"\"\""
	case "$A" in
	*.py)
		< ~/code/python/hello.py sed -e 's/hello.py/'"$name"'/' > "$A"
		;;
	*.pl)
		lecho "#!/usr/bin/perl -w" "$header_comment" "use strict;" "use warnings;" "" > "$A"
		;;
	*.js)
		lecho "#!/usr/bin/env node" "$header_comment_slashes" > "$A"
		;;
	*.rb)
		lecho "#!/usr/bin/env ruby" "$header_comment" > "$A"
		;;
	*.lua)
		lecho "#!/usr/bin/env lua" "$header_comment" > "$A"
		;;
	*.txt|*.md)
		;;
	*.make)
		lecho "#!/usr/bin/make -f" "$header_comment" > "$A"
		;;
	*.c)
		< ~/code/c/hello.c sed -e 's/hello.c/'"$name"'/' > "$A"
		;;
	*.cpp)
		< ~/code/c/hello.cpp sed -e 's/hello.cpp/'"$name"'/' > "$A"
		;;
	*)
		lecho "#!/bin/bash -eu" "$header_comment" > "$A"
		;;
	esac
done
${VXED:-vi -o} "$@"
for A; do
	if [ ! -s "$A" ]; then
		rm -f "$A"
	fi
done
