diff --git a/bin/tm b/bin/tm new file mode 100755 index 0000000..72ed72f --- /dev/null +++ b/bin/tm @@ -0,0 +1,74 @@ +#!/bin/bash +# Configuration +DATADIR=$HOME/data/screen + +# Variables +#DRY=echo +DRY= +CLEAN=0 + +usage () { + echo "Usage: $(basename $0) [-n] name" + echo + echo "-n - perform dry run, do nothing" +} + +if [ $# -lt 1 ] ; then + usage + exit 1 +fi + +while [ $# -gt 0 ] ; do + # Store how many options we have now + OPTCOUNT=$# + OPT=$1 + + # Consume options if we recognise them + if [ "$OPT" == "-n" ] ; then + DRY=echo + shift + fi + if [ "$OPT" == "-h" ] ; then + usage + exit 0 + fi + if [ "$OPT" == "-l" ] ; then + tmux ls + exit $? + fi + + # Bit of a hack, but it works. + if [ $# == $OPTCOUNT ] ; then + break + fi +done + +if [ "$TMUX" != "" ] ; then + echo "You're already in a tmux session; detach first." + exit 1 +fi + +TARGET=$1 +shift +if [ "$TARGET" == "" ] ; then + echo "You didn't specify a target." + usage + exit 1 +fi + +echo Looking for $TARGET + +MATCH=$(tmux ls | grep "$TARGET") +if [ "$MATCH" == "" ] ; then + # Use the name as what they passed. + SESSION_NAME="$TARGET" + COMMAND="new -s $SESSION_NAME" +else + # Pull out the existing session name. + SESSION_NAME=$(echo "$MATCH" | sed -e 's/:.*$//') + COMMAND="at -t $SESSION_NAME" +fi + +if [ "$COMMAND" != "" ] ; then + $DRY tmux $COMMAND +fi