#!/bin/bash
#
# A simple script to get the latest kernel from the 
# ftp.us.kernel.org archive.  This is mainly a 
# demonstration of some simple looping and the 
# here-document facility of bash.
#
# The first set of lines sets up the .netrc file 
# to contain an appropriate macro for fetching a 
# kernel from the kernel archive.  It assumes that
# $USER on the local machine is the same as $USER on
# your isp account, and that it can authenticate 
# accordingly.  This script assumes that it is 
# being run from the home directory of the person
# downloading the new kernel, and that that person
# is not root. 
#
# This is an example of a 'here-document'.  The lines 
# following the 'cat ...' command are copied directly
# to the '.netrc' file.  The '<<-' is what signifies
# a here-document, and will strip off leading tabs.
# A bare '<<' will merely copy the text following 
# up until the end marker (EOF in this case).
# Surrounding the end marker in single or double
# quotes prevents variable interpolation.
#
cat > .netrc <<-EOF
	machine ftp.us.kernel.org
	login anonymous
	password ${USER}@
	macdef init
	cd pub/linux/kernel/v2.2
	type binary
	get linux-2.2.9.tar.gz
	quit
	
EOF
#
# .netrc needs to be u=rw, go=nothing or else the ftp
# will refuse to use it.
#
chmod 600 .netrc
ppp-on
#
# After starting the pppd, we have to wait until the
# link is actually up and running.  Because of the 
# way ppp is usually set up, ppp-on will exit successfully
# before the link is up and running, and prematurely
# executing the ftp command will hang the script,
# because the ftp macro will fail and drop to the
# command prompt.  Instead, we make this script 
# sleep until we can find the route that we want.
#
while ! /sbin/route|grep ppp0|grep default; do
	sleep 10
done
#
# The '-i' option to ftp disables interactive usage.
#
ftp -i ftp.us.kernel.org
ppp-off
cp .netrc.back .netrc