1#!/bin/sh
2# @(#)wget.sh  1.3 05/05/30 Copyright 2005 J. Schilling
3###########################################################################
4# Written 2005 by J. Schilling
5###########################################################################
6# A simulation of wget using ftp
7###########################################################################
8# The contents of this file are subject to the terms of the
9# Common Development and Distribution License, Version 1.0 only
10# (the "License").  You may not use this file except in compliance
11# with the License.
12#
13# See the file CDDL.Schily.txt in this distribution for details.
14# A copy of the CDDL is also available via the Internet at
15# http://www.opensource.org/licenses/cddl1.txt
16#
17# When distributing Covered Code, include this CDDL HEADER in each
18# file and include the License file CDDL.Schily.txt from this distribution.
19###########################################################################
20
21excod=255
22
23trap 'rm -fr /tmp/.d.$$ ; exit 1' 1 2 15
24trap 'rm -fr /tmp/.d.$$ ; exit $excode' 0
25
26if [ $# -lt 1 ]; then
27	echo "Usage: wget.sh [-d] URL"
28	echo "Options:"
29	echo "	-d	Print a listing of the dir part and exit"
30	echo
31	echo "Only ftp:// type URLs are supported"
32	exit 1
33fi
34
35dodir=FALSE
36if [ ."$1" = .-d ]; then
37	dodir=TRUE
38	shift
39fi
40URL="$1"
41rest=`echo "$URL" | sed -e 's,^ftp://,,'`
42if [ "$URL" = "$rest" ]; then
43	echo "Unsupported protocol in" "$URL"
44	exit 1
45fi
46
47HOST=`echo "$rest" | sed -e 's,/.*,,'`
48FILE=`echo "$rest" | sed -e 's,^[^/]*/,,'`
49#echo HOST "$HOST"
50#echo FILE "$FILE"
51
52DNAME=`echo "$FILE" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
53FNAME=`echo "$FILE" | sed -e 's,.*/,,'`
54#echo DNAME "$DNAME"
55#echo FNAME "$FNAME"
56
57mkdir /tmp/.d.$$
58cat <<EOF > /tmp/.d.$$/.netrc
59machine "$HOST"  login ftp password wget.sh@Makefiles.Schily.build
60EOF
61chmod 600 /tmp/.d.$$/.netrc
62
63if [ $dodir = TRUE ]; then
64	HOME=/tmp/.d.$$ ftp "$HOST" <<EOF
65	bin
66	passive on
67	cd "$DNAME"
68	dir
69	bye
70EOF
71excode=$?
72
73else
74
75	HOME=/tmp/.d.$$ ftp "$HOST" <<EOF
76	bin
77	passive on
78	cd "$DNAME"
79	dir "$FNAME"
80	hash on
81	verbose on
82	get "$FNAME"
83	bye
84EOF
85excode=$?
86fi
87