#!/usr/bin/env bash

TERMCMD=${TERMCMD:-urxvto}

PROGNAME=$(basename $0)

OPTSTRING="${PROGNAME} (-x) [ -h | -l | -L host | -z | -c name | -n name | -N host | -e name tab command ]"

if [[ $# -eq 0 ]] ; then
    echo ${OPTSTRING} >&2
    exit 1
fi

XWINFLAG=false

while getopts "c:n:e:L:N:zlxh" OPT
do
    # shift
    case "$OPT" in
        x)
            XWINFLAG=true
            ;;
        z)
            NAME=$(tmux list-sessions -F '#S' | grep '^term' | sort -r | head -n 1)
            if [ -n "${NAME}" ] ; then
                NUM=$(echo $NAME | cut -d- -f 2)
                NUM=$(( NUM + 1 ))
                NAME="term-${NUM}"
            else
                NAME=term-0
            fi
            ${TERMCMD} -e tmux new -s "${NAME}"
            ;;
        l)
            if [[ $XWINFLAG != "true" ]] ; then
                tmux list-sessions -F '#S'
            else
                if [[ -e `which rofi` ]] ; then
                    NAME=`tmux list-sessions -F '#S' | rofi -dmenu -p "screen session"`
                else
                    NAME=`tmux list-sessions -F '#S' | dmenu -p "screen session:"`
                fi
                [[ $NAME != "" ]] && ${TERMCMD} -e tmux attach -t $NAME
            fi
            ;;
	L)
	    if [[ $XWINFLAG != "true" ]] ; then
		tmux list-sessions -F '#S' | grep "${OPTARG}"
	    else
		if [[ -e `which rofi` ]] ; then
		    NAME=`tmux list-sessions -F '#S' | grep "${OPTARG}" | rofi -dmenu -p "screen session"`
		else
		    NAME=`tmux list-sessions -F '#S' | grep "${OPTARG}" | dmenu -p "screen session:"`
		fi
		[[ $NAME != "" ]] && ${TERMCMD} -e tmux attach -t $NAME
	    fi
	    ;;
        c)
            if [[ $XWINFLAG != "true" ]] ; then
                tmux attach -t "${OPTARG}"
            else
                urxtvo -e "tmux attach -t \"${OPTARG}\""
            fi
            ;;
        n)
            if [[ $XWINFLAG != "true" ]] ; then
                tmux new -s "${OPTARG}"
            else
                ${TERMCMD} -e tmu -n "${OPTARG}"
            fi
            ;;
	N)
	    
	    NAME=$(tmux list-sessions -F '#S' | grep "^${OPTARG}" | sort -r | head -n 1)
            if [ -n "${NAME}" ] ; then
		NUM=$(echo $NAME | cut -d- -f 2)
		NUM=$(( NUM + 1 ))
		NAME="${OPTARG}-${NUM}"
            else
		NAME="${OPTARG}-0"
            fi
	    tmux new -s "${NAME}"
	    ;;
        e)
            OPTIND1=$(( OPTIND + 1 ))
            TAB=${!OPTIND}
            OPTIND1=$(( OPTIND + 1 ))
            COMMAND=${!OPTIND1}
            if tmux has-session -t "${OPTARG}" ; then
                TMUX='' tmux attach-session -t "${OPTARG}" \; new-window -d -n "${TAB}" "${COMMAND}" \; detach-client
            else
                TMUX='' tmux new -s "${OPTARG}" -n "${TAB}" -d "${COMMAND}"
            fi
            exit
            ;;
        h)
            echo ${OPTSTRING} >&2
            cat <<HELP >&2

-h            Show this help.
-x            Turn on X-windows interaction.
-z            Create a new, unnamed screen session with X Terminal.
-l            List screen sessions (with -x, show in ROFI).
-l HOST       List sessions on HOST.
-c NAME       Connect (as an additional screen) to session "name".
-n NAME       Create and connect to a new screen session "name".
-N HOST       Create and connect to a new numbered screen session for HOST.
-e NAME TAB COMMAND
              Create a tab named "tab" running the command "command" in session "name".
HELP
            exit
            ;;
    esac
done
