aboutsummaryrefslogtreecommitdiffstats
path: root/examples/scripts.v2/frcp
blob: 572aa7bc25f1221f3f0e9a64cc422697ef28a06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#! /bin/bash
#
# original from:
#
# @(#) frcp.ksh 2.2 93/11/14
# 92/06/29 john h. dubois iii (john@armory.com)
# 92/10/14 Cleaned up, improved, added -d and -r options
# 92/11/11 Made work with a dest of '.'
# 93/07/09 Added -l and -n options, & login as anonymous if no .netrc entry
# 93/11/14 Use either passwd or password in .netrc, since ftp does.
#
# conversion to bash v2 syntax by Chet Ramey
#
# frcp: ftp front end with rcp-like syntax.
# Note: requires any machine names given to be listed with
#       user and password in .netrc.  If not, anonymous FTP is
#	done.
#
# full path to ftp binary
if [ -x /usr/bin/ftp ]; then
	FTP=/usr/bin/ftp;
elif [ -x /usr/ucb/ftp ]; then
	FTP=/usr/ucb/ftp
else
	FTP=ftp
fi

istrue()
{
	test 0 -ne "$1"
}
isfalse()
{
	test 0 -eq "$1"
}

# For each filename given, put the filename in filename[n]
# and the machine it is on in machine[n].
function SplitNames {
    typeset file
    typeset -i i=1

    unset filename[*] machine[*]
    for file; do
	case "$file" in
	*:*) machine[i]=${file%%:*} ;;
	*)   machine[i]=$LocalMach ;;
	esac
	filename[i]=${file#*:}
	let i+=1
    done
}

function verboseprint {
    echo "$@"
    echo "$@" 1>&2
}

function MakeDir {
    OFS=$IFS
    local IFS=/ dir component

    case "$1" in
    /*)	;;
     *) dir=.
    esac
    set -- $1
    IFS=$OFS
    for component; do
	dir=$dir/$component
	if [ ! -d "$dir" ]; then
	    if mkdir "$dir"; then :; else
		echo "Could not make directory $dir." >&2
		return 1
	    fi
	fi
    done
    return 0
}

lastisdot ()
{
	case "$1" in
	*/.|*/..)	return 0;;
	*)	return 1;;
	esac
}

# CopyFiles: issue ftp(TC) commands to copy files.
# Usage: CopyFiles [sourcemachine:]sourcepath ... [destmachine:]destpath
# Global vars:
# Uses LocalMach (should be name of local machine)
# Sets global arrs machine[]/filename[]
function CopyFiles {
    unset machine[*] filename[*]

    SplitNames "$@"	# split names into filename[1..n] and machine[1..n]

    local DestMach=${machine[$#]}	# Machine to copy files to
    local DestPath=${filename[$#]} 	# Destination file/dir

    unset machine[$#] filename[$#]

    [ -z "$DestPath" ] && DestPath=.	# dest was given as machine:

    # Try to determine if destination should be a directory
    # so that it can be forced to be a directory.

    case "$DestPath" in
    */)	;;	# don't add / if trailing / already present
    *)  if [ $# -gt 2 ] || # if more than two args given, last must be a dir
	    # If dest in on local machine, check whether it is a directory
    	   [ $DestMach = $LocalMach ] && [ -d "$DestPath" ] || 
	    # If dest ends with . or .., it is a directory
	   lastisdot "$DestPath"
	then
		DestPath=$DestPath/
	fi ;;
    esac

    # If one of the above tests made us think dest is a directory,
    # but it isn't, complain
    case "$DestPath" in
    */)	if [ "$DestMach" = "$LocalMach" ] && [ ! -d "$DestPath" ]; then
		echo "Destination is not a directory." 1>&2
		exit 1
        fi ;;
    esac

    DoCopy "$DestMach" "$DestPath"
}

# Usage: OpenMachine machine-name
# Emits login sequence or doesn't, depending on .netrc file and global
# variables anon and noanon
OpenMachine ()
{
    local machine=$1 netrc=$HOME/.netrc user= password=

    if isfalse $anon && [ -r $netrc ]; then
	set -- $(gawk '
	/machine (.* )?'"$machine"'($| )/,/^ *$/ {
	    Fields[$1] = $2
	    if ("passwd" in Fields)
		Fields["password"] = Fields["passwd"]
	    if ("login" in Fields && "password" in Fields) {
		print Fields["login"] " " Fields["password"]
		exit
	    }
	}
	' $netrc )
	user=$1
	password=$2
    fi
    if [ -z "$password" ]; then
	if istrue $noanon; then
	    echo "No .netrc entry for machine $machine" 1>&2
	    exit 1
	fi
	user=anonymous
	password=$USER@$LocalMach
    fi
    verboseprint open $machine
    echo user $user "*******" 1>&2
    echo user $user $password
}

# Usage: DoCopy destination-machine destination-path
# Copies the files in global arrs machine[]/filename[] to the given dest
# Global vars:
# Uses machine[], filename[], LocalMach, check
DoCopy ()
{
    local DestMach=$1
    local DestPath=$2
    local OpenMach	# Machine that connection is currently open to
    local OWD=$PWD SourceMach SourceFile
    local FileName
    typeset -i i=1

    while [ $i -le ${#machine[*]} ]; do
	istrue $check && verboseprint "runique"

	SourceMach=${machine[i]}
	SourceFile=${filename[i]}

	DestFile=$DestPath
	# if DestPath is a dir,
	# add source filename to it without source path
	case "$DestFile" in
	*/)	DestFile=$DestFile${SourceFile##*/} ;;
	esac

	if [ $SourceMach = $LocalMach ]; then
	    if [ $DestMach != "$OpenMach" ]; then
		OpenMachine $DestMach
		OpenMach=$DestMach
	    fi
	    verboseprint put $SourceFile $DestFile
	elif [ $DestMach = $LocalMach ]; then
	    if istrue $check && [ -f "$DestFile" ]; then
		echo "$DestFile already exists." 1>&2
		continue
	    fi
	    # If destination is on local machine,
	    # the dest will be a full dir/filename
	    if istrue $createdirs; then
		MakeDir "${DestFile%/*}" || continue
	    fi
	    if [ $SourceMach != "$OpenMach" ]; then
		OpenMachine $SourceMach
		OpenMach=$SourceMach
	    fi
	    # If source filename has wildcards ([, ], *, ?) do an mget
	    case "$SourceFile" in
	    \[*\]|*\**|*\?*)
		verboseprint lcd "$DestFile"
		verboseprint mget "$SourceFile"
		verboseprint lcd $OWD ;;
	    *)  verboseprint get "$SourceFile" "$DestFile" ;;
	    esac
	else
	    echo "Neither source machine \"$SourceMach\" "\
"nor destination machine \"$DestMach\" is local." 1>&2
	fi
	let i+=1
    done
}

# Start of main program
name=${0##*/}

if [ "$1" = -h ]; then
    echo \
"$name: do ftp transfers using rcp-style parameters.
Usage: $name <source> <destpath>   or   $name <source> [<source> ...] <destdir>
At least one of <source> and <destpath> must be the local system.
A remote filename is given as machinename:filename
If remote filenames contain wildcards, they will be globbed on the remote
machine.  Make sure they are quoted when $name is invoked.
If the invoking user's .netrc file (see ftp(TC)) contains an entry for the
remote system with a login and password supplied, $name will log in using
the given login and password.  If not, $name will login in as user
anonymous and with the user@localsystem as the password.
Options:
-c: check: do not overwrite files.
-d: create directories as needed.
-f: force: overwrite files (default).
-h: print this help.
-l: fail if there is no entry with login and password for the remote system,
    instead of logging in as anonymous.
-n: log in as anonymous even if there is an entry for the remote system in
    the user's .netrc file.
-r: read source/dest filename pairs from the standard input,
    one pair per line, and copy files accordingly."
    exit 0
fi

typeset -i check=0 createdirs=0 readinput=0 anon=0 noanon=0

while getopts :cdflnr Option
do
    case "$Option" in
    c) check=1;;
    d) createdirs=1;;
    f) check=0;;
    l) noanon=1;;
    n) anon=1;;
    r) readinput=1;;
    \?) echo "$OPTARG: invalid option."; exit 1;;
    esac
done

shift $((OPTIND-1))

LocalMach=`hostname` 

if istrue $readinput; then
    while read line; do
	CopyFiles $line
    done | $FTP -nv
else
    if [ $# -lt 2 ]; then
	echo "$name: Not enough arguments.  Use -h for help." 1>&2
	exit
    fi
    CopyFiles "$@" | $FTP -nv
fi