#!/bin/sh

[[ $# -lt 1 ]] && exit 1

SCRIPT_DIR=${HOME}/bin

TYPE=$1
case "$TYPE" in
    -bash)
        TYPE=".bash"
        SHEBANG="#!/bin/bash"
        shift
        ;;
    -pl)
        TYPE=".pl"
        SHEBANG="#!/usr/bin/env perl -w\nuse strict;"
        shift
        ;;
    -py)
        TYPE=".py"
        SHEBANG="#!/usr/bin/env python"
        shift
        ;;
    -rb)
        TYPE=".rb"
        SHEBANG="#!/usr/bin/env ruby"
        shift
        ;;
    -sh)
        TYPE=".sh"
        SHEBANG="#!/bin/sh"
        shift
        ;;
    -zsh)
        TYPE=".zsh"
        SHEBANG="#!/usr/bin/env zsh"
        shift
        ;;
    *)
        TYPE=""
        ;;
esac

while [[ -n ${1} ]] ; do
	SCRIPT=${SCRIPT_DIR}/${1}${TYPE}
    if [[ -n ${TYPE} ]] && [[ ! -e ${SCRIPT} ]]; then
        echo "${SHEBANG}" > ${SCRIPT}
        chmod +x ${SCRIPT}
    elif [[ ! -e ${SCRIPT} ]]; then
        touch ${SCRIPT}
        chmod +x ${SCRIPT}
    elif [[ -w ${SCRIPT} ]] ; then
		echo "Script ${SCRIPT} already exists, opening to edit anyways." >&2
    else
        echo "Script ${SCRIPT} is not writable, ignoring." >&2
        SCRIPT=""
	fi
	SCRIPTS="${SCRIPTS} ${SCRIPT}"
	shift
done

${EDITOR} ${SCRIPTS}
