#!/bin/csh -f
# pull_protected_file_from_server
# -----------------------------------------------------------------------------
# Shell script to pull a file from a protected location on a server
# Run locally by Fred to pull a remote config file from a server.
# -----------------------------------------------------------------------------
# Usage:  See Usage section below or run with -h or --help option to see usage.
# Assumptions:
# - Assumes sudo rights on the remote server
# - Assumes the remote file already exists.
# Effects:
# - Copies the file to the home directory on the remote server, changes owner
#   of the copy to fred:fred, pulls it from the server to a local copy, and
#   and then deletes the remote copy. Shows differences and prompts before
#   finally moving the local copy into the specified local file.
# Notes:
# Implementation Notes:
# Portability Issues:
# - Not very portable.  Performs a specific useful task in Fred's typical
#   environment.
# Revision History:
#   $Log$
# -----------------------------------------------------------------------------

if ($#argv < 3 || "$1" == "-h" || "$1" == "--help") then
   echo "Usage: $0:t host remote_file_name local_file_name"
   echo "host            = Name or IP of server."
   echo "                  Example: 174.129.10.250 or bristle.com"
   echo "remote_file_name = Name of remote file to copy from"
   echo "                  Example: /etc/hosts"
   echo "local_file_name = Name of local file to create or overwrite"
   echo "                  Example: hosts or ./etc/hosts"
   exit 1
endif

set host=$1
set remote_file_name=$2
set local_file_name=$3

set temp_file_name="$0:t.tmp"

set echo
ssh -t ${host} sudo cp -iv ${remote_file_name} ${temp_file_name}
ssh -t ${host} sudo chown fred:fred ${temp_file_name}
scp ${host}:${temp_file_name} ${temp_file_name}
ssh -t ${host} rm -fv ${temp_file_name}
windiff ${temp_file_name} ${local_file_name}
cp -iv ${temp_file_name} ${local_file_name}
rm -fv ${temp_file_name}