#!/bin/sh
# $Id: scmd,v 1.3 2000/06/05 23:03:25 dmetz Exp $
# $crtd:  by  Derald Metzger  on  990517 $
# $cmnt:  shotgun ssh cmds to other hosts
#           Copyright (c) 1998 1999  Derald Metzger 
#  This file is part of the cfm pkg (GPL).
# $

base=`basename $0`
echo=echo

case $SHELL in
  *bash|sh|*/sh)
    shell=sh
    env_fil=$HOME/.bash_profile
  ;;
  *tcsh|csh|*/csh)
    shell=csh
    env_fil=$HOME/.cshrc
  ;;
  *) echo "### unsupported \`\$SHELL=$SHELL'. Exiting"
    exit
  ;;
esac

usage="
DESCR:
  Execute cmds on one or more remote hosts with the user's env.
  \$SHELL and \$HOME of the executing account are used as defaults
  to locate and source the login env on the remote host.
USAGE:
  $base [-s shell] [-e env_fil] cmd [;cmd] host [ ... ]
     env_fil  env file to source before executing cmd
     cmd ;... cmds to execute on remote hosts. quotes may be rqd 
     host ... list of hosts on which to execute the cmd;
"

while getopts e:s: c
do
  case $c in
    e) env_fil=$OPTARG ;;
    s) shell=$OPTARG ;;
    \?) $echo "$usage"; exit 2; ;;
  esac
done
shift `expr $OPTIND - 1`

if [ $# -lt 2 ]; then echo "### $base: insufficient args $usage"; exit 1; fi

case $shell in
  sh) source=. ;;
  csh) source=source ;;
esac

cmd=$1
shift

for i
do
  echo "## ssh $i \"$source $env_fil; $cmd\""
  ssh $i "$source $env_fil; $cmd"
done
