#!/bin/sh

# ===============================================================================
# Copyright (c) 2016, Joseph Mingrone.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ===============================================================================

# scct - Simple Cluster_SSH Clone using Tmux
#
# Run scct -h or visit the GitHub page for an overview.
#
# https://github.com/Jehops/scct
#
# scct was influenced by Cluster SSH, tmux-cssh, and a script written by Andrew
# Fresh.
#

# scct - Simple Cluster_SSH Clone using Tmux

fatal () {
    echo "FATAL: $*" > /dev/stderr
    exit 1
}

help () {
    readonly version=0.1

    cat <<EOF
NAME
   ${0##*/} -- Simple Cluster_SSH Clone using Tmux

SYNOPSIS
   ${0##*/} [[[user@]host1] ...]
   ${0##*/} -h | --help

DESCRIPTION
Quickly create multiple tmux panes, each with an ssh connection to a specified
host and optionally synchronize input.  Input synchronization can be toggled
with prefix S.  If no host list is specified, the hl variable set in ~/.scct is
used.

AUTHOR
   Joseph Mingrone <jrm@ftfl.ca>

   scct was influenced by Cluster SSH, tmux-cssh, and a script written by Andrew
   Fresh.

VERSION
   ${0##*/} version $version

EOF
    exit 0
}

if [ "$1" ] && ( [ "$1" = '-h' ] || [ "$1" = '--help' ] ); then
    help
fi

[ -r ~/.scct ] && . ~/.scct

if [ -z "$*" ]; then
     if [ -z "$hl" ]; then
         help
     fi
else
    hl="$*"
fi

TMUX_TMPDIR=$(mktemp -d -t scct)
[ -z "$TMUX_TMPDIR" ] && fatal "Failed to create temporary directory."
trap 'rm -rf $TMUX_TMPDIR; exit 1' 0 1 2 15
export TMUX_TMPDIR

tcmd="-2 new -d"
for h in $hl; do
    tmux $tcmd ssh "$h" \; select-layout tiled
    tcmd='splitw'
done

tmux set exit-unattached on \; att \; set synchronize-panes \; \
     bind S set synchronize-panes
