#!/bin/bash
#
# ScreenCSSH - Screen-based cluster SSH
# Version: 1.0
# Author: Travis Estill (travisce.com)
# Copyright (c) 2013 Travis Estill
# License: Distributed under the Lesser General Public License
#   (LGPL) http://www.gnu.org/copyleft/lesser.html
#   This program is distributed in the hope that it will be useful
#   - WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# ScreenCSSH command prompt

function ctrlc {
  screen -S "$SCREEN" -X at "server#" stuff "$(printf \\003)"
}

function ctrlz {
  screen -S "$SCREEN" -X at "server#" stuff "$(printf \\032)"
}

if [ -z "$SCREEN" ]
then
  echo "SCREEN is not defined" 1>&2
  exit 1
fi

# Allows signal codes to be passed through
trap ctrlc INT
trap ctrlz TSTP

clear
echo -n 'Enter command: '

IFS=''
while true
do
  read -s -r -n1 cmd
  clear
  echo -n 'Enter command: '
  [ "$cmd" == '' ] && cmd=$(printf \\r)
  screen -S "$SCREEN" -X at "server#" stuff "$cmd"
done
