#!/bin/sh
# /home/cymbala/bin/cio.sh
#
# SYNTAX:
# ssh-agent
# ssh-add
# sh ~/bin/cio.sh (remote_site) (remote_user) (remote_group) (paths) out (filename)
# ( ...make edits to local copy... )
# sh ~/bin/cio.sh (remote_site) (remote_user) (remote_group) (paths) in (filename)
#
# =============================================================================
# EXAMPLE: sh /home/cymbala/bin/cio.sh www.foo me tribe archive/devel out index.htm
#
# =============================================================================
# SAMPLE:
#
# me@debian:~$ sh ~/bin/cio.sh www.foo me tribe archive/devel out preface2.htm
#
# Checking out: preface2.htm ...
# preface2.htm.checked-out. | 11 KB | 2.8 kB/s | ETA: 00:00:00 | 100%
#
# /home/me/www.foo/archive/devel/preface2.htm
# /home/me/www.foo/archive/devel/preface2.htm.checked-out.me
#
# Done.
# me@debian:~$
# me@debian:~$ sh ~/bin/cio.sh www.foo me tribe archive/devel in preface2.htm
#
# Checking in: preface2.htm ...
# preface2.htm | 11 KB | 11.3 kB/s | ETA: 00:00:00 | 100%
#
# Done.
# me@debian:~$
# me@debian:~$
#
# =============================================================================
public_site=$1
public_user=$2
group=$3
target_dirs=$4
action_io=$5
filename=$6
alter_ego=${public_user}@${public_site}
#
this_script=~/bin/cio.sh
public_html=/www/public_html
local_html=~/$public_site
copy_program=scp
copy_program_opts='-p'
function checker {
echo ""
# If filename is part of target_dirs and filename is empty,
# move filename from target_dirs to filename.
if [ ".${target_dirs##*.}" == ".htm" ]; then
if [ "$filename" == "" ]; then
echo "$target_dirs --> $filename"
filename="${target_dirs##*/}"
target_dirs="${target_dirs%/*}"
fi
fi
echo "$target_dirs --> $filename"
if [ "$filename" == "" ]; then
echo ""
echo "Missing argument (a.k.a. positional parameter).";
egrep "EXAMPLE: sh $this_script" $this_script
exit 1
fi
echo "Checking ${action_io}: ${filename} ..."
#
# Both check-out and check-in involve three steps:
# ----------
# CHECK-OUT:
# 1. rename file on remote host (original name to checked-out name).
# 2. download file (checked-out name to checked-out name).
# 3. copy file on local host (checked-out name to original name).
# ---------
# CHECK-IN:
# 1. upload file (original name to original name).
# 2. delete checked-out on remote host (checked-out name).
# 3. delete LINK on local host (checked-out name).
#
# CHECK-IN COULD INCLUDE a fourth step (delete original name
# on local computer), but not deleting it builds a mirror of
# Web pages on local hard drive.
pathfile=$target_dirs/$filename
checked_pathfile=${pathfile}.checked-out.${public_user}
#
original=$public_html/$pathfile
lent=$local_html/$pathfile
#
on_loan=$public_html/$checked_pathfile
link=$local_html/$checked_pathfile
case $action_io in
out)
if [ -f $link ]; then
echo "Already checked-out: $lent"
exit 1
fi
#
ssh -l $public_user $public_site \
mv $original \
$on_loan
if [ $? != 0 ]; then exit 1; fi
#
mkdir -p ${link%/*}
$copy_program $copy_program_opts \
${alter_ego}:$on_loan \
$link
#
# Uses '-f' to prevent prompting within a for-loop.
cp -pf $link $lent
ls -1 $lent $link
echo ""
;;
in)
if [ ! -f $link ]; then
echo "Nothing to check-in!"
exit 1
fi
#
# AT FIRST: ``$copy_program ... $lent ${alter_ego}:$on_loan''
# scp: .../preface2.htm.checked-out.rjc: set mode: Operation not permitted
#
# BUT ``mv $on_loan $original'' was not working since owner
# was "marxists" and not "rcymbala".
#
$copy_program $copy_program_opts $lent ${alter_ego}:$original
if [ $? != 0 ]; then exit 1; fi
#
ssh -l $public_user $public_site \
"rm -f $on_loan ; chgrp $group $original; chmod g+w $original"
rm -f $link
;;
*)
echo " INVALID MODE: $action_io"
exit 1
;;
esac
# upload_script >/dev/null 2>&1
echo "Done."
}
function upload_script {
echo "Uploading this script..."
$copy_program $copy_program_opts \
$this_script ${alter_ego}:$public_html/$target_dirs/${this_script##*/}
}
# =============================================================================
# MAIN:
checker
# This script could check whether ssh-agent is running...
# ~$ set | egrep -i ssh
# SSH_AGENT_PID=499
# SSH_AUTH_SOCK=/tmp/ssh-cymbala/ssh-472-agent
# Make this script -rwx------
# -------------------------------------------------------
# -------------------------------------------------------
# -------------------------------------------------------
#
# BATCH-MODE:
#
# rcymbala@mia:~$ cd /www/public_html/archive/lenin/works/; for year in `ls -d 1[89]*`; do find $year -name '*htm*' | xargs -i awk 'length > 110 { print FILENAME, length; exit;}' '{}'; done > ~/length_gt_110.rpt
#
# cymbala@debian:~$ scp rcymbala@www.marxists.org:length_gt_110.rpt ~/length_gt_110.rpt
#
# cymbala@debian:~$ for obj in `egrep '1916/imp-h' ~/length_gt_110.rpt | awk '{print $1;}'`; do sh ~/bin/cio.sh www.marxists.org rcymbala lia archive/lenin/works/$obj out; done
#
# (make edits)
#
# cymbala@debian:~$ for obj in `egrep '1916/imp-h' ~/length_gt_110.rpt | awk '{print $1;}'`; do sh ~/bin/cio.sh www.marxists.org rcymbala lia archive/lenin/works/$obj in; done
#
###
#