#!/bin/ksh
#Copy a file to the ~/public_html directory so it can be seen
#on the web. Sample use: post filename
if [[ $# -ne 1 ]]
then
echo $0: requires exactly one argument 1>&2
exit 1
fi
if [[ ! -f $1 ]]
then
echo $0: there is no file named $1 1>&2
exit 2
fi
if [[ ! -r $1 ]]
then
echo $0: you must have r permission for the file $1 1>&2
exit 3
fi
if [[ ! -d ~/public_html ]]
then
if [[ -e ~/public_html ]]
then
echo $0: you already have a non-directory named ~/public_html \
1>&2
exit 4
fi
if [[ ! -w ~ ]]
then
echo $0: you have no permission to create ~/public_html 1>&2
exit 5
fi
if ! mkdir ~/public_html
then
echo $0: failed to create ~/public_html 1>&2
exit 6
fi
fi
if [[ ! -w ~/public_html ]]
then
if ! chmod 755 ~/public_html
echo $0: failed to chmod 755 ~/public_html 1>&2
exit 7
fi
fi
#$newfilename is the full pathname of the copy we will create in ~/public_html.
newfilename=~/public_html/${1##*/} #Handout 4, pp. 3-4 for ##
if [[ -e $newfilename ]]
then
echo $0: $newfilename already exists and I will not harm it 1>&2
exit 8
fi
if ! cp $1 $newfilename
echo $0: failed to create $newfilename 1>&2
exit 9
fi
if ! chmod 444 $newfilename #r--r--r--
then
echo $0: failed to chmod 444 $newfilename 1>&2
exit 10
fi
exit 0