#! /bin/bash
init() {
HOST="http://tremmap.omgwtf.nl/"
LIST="tremmapdownload1.php"
HOMEPATH="$HOME/.tremulous/base/"
#BASEPATH=""
if [ ! $BASEPATH ]; then # set BASEPATH above to override
BASEPATH=`which tremulous 2>/dev/null`;
if [ -L "$BASEPATH" ]; then # symlink
BASEPATH=`stat -c %N $BASEPATH \
|sed 's/.*-> \`\(.*\)\/[^/]\+/\1\/base\//'`;
elif [ -x "$BASEPATH" ]; then # file, just get the path
BASEPATH=`echo $BASEPATH |sed 's/\/[^/]\+$/\/base\//'`;
else
echo "WARNING! Cannot locate Tremulous! Set BASEPATH manually.";
fi;
fi;
if [ $UID -eq 0 ]; then # root installs to BASEPATH
if [ ! $BASEPATH ]; then
echo "Error: Set BASEPATH before running as root.";
exit 1;
fi;
cd $BASEPATH;
else # everybody else to his/her home directory
cd $HOMEPATH;
fi;
FILES=`wget $HOST$LIST -q -O - |sed 's/\r//g' |tail -n +4 |xargs -n3 \
|sed 's/[[:blank:]]/:/g'`; # get maplist
if [ ! "$FILES" ]; then
echo "Error: Could not receive maplist.";
exit 1;
fi;
}
checksum() {
if [ ! "$MD5SUM" ]; then MD5SUM=`which md5sum 2>/dev/null`; fi
if [ ! "$SHA1SUM" ]; then SHA1SUM=`which sha1sum 2>/dev/null`; fi
if [ ! "$MD5SUM$SHA1SUM" ]; then
echo "WARNING: No checksum tools found. Install md5sum or sha1sum.";
return 0;
fi;
if [ -x $MD5SUM ]; then # md5sum
echo $1 | sed 's/^\(.*\)[[:blank:]]\+\([[:alnum:]]\+\)[[:blank:]]\+\([[:alnum:]]\+\)$/\2 \1/' \
| $MD5SUM -c --status
if [ $? -ne 0 ]; then
return 1; # failed
fi;
fi;
if [ -x $SHA1SUM ]; then # sha1sum
echo $1 | sed 's/^\(.*\)[[:blank:]]\+\([[:alnum:]]\+\)[[:blank:]]\+\([[:alnum:]]\+\)$/\3 \1/' \
| $SHA1SUM -c --status
if [ $? -ne 0 ]; then
return 1; # failed
fi;
fi;
return 0; # passed
}
getfname() {
echo $1 |sed 's/[[:blank:]]\+[[:alnum:]]\+[[:blank:]]\+[[:alnum:]]\+$//';
}
verifyfile() {
if [ ! "$FILES" ]; then init; fi
fname=`getfname $1`;
if [ -f "$fname" ] && checksum "$1"; then
return 0; # local checksum passed
fi
if [ $UID -ne 0 ] && [ "$BASEPATH" ] && [ -f "$BASEPATH$fname" ]; then
if checksum "$BASEPATH$1"; then
return 0; # BASEPATH checksum passed
fi
fi
return 1; # failed or missing
}
verify() {
if [ ! "$FILES" ]; then init; fi
ok=0;
broken=0;
missing=0;
for line in $FILES; do
line=`echo $line |sed 's/:/ /g'`;
fname=`getfname $line`;
if [ -f "$fname" ] || [ -f "$BASEPATH$fname" ]; then
echo -n "$fname ";
if verifyfile "$line"; then # file exists, checksum
let ok++;
echo "OK";
else # failed
let broken++;
echo "KO";
fi
else # file missing
let missing++;
echo "$fname not found";
fi
done
echo "$broken maps broken, $missing maps missing, $ok maps OK"
}
instmaps() {
if [ ! "$FILES" ]; then init; fi
broken=0;
for line in $FILES; do
line=`echo $line |sed 's/:/ /g'`;
fname=`getfname $line`;
if [ "$1" ] && [ ! "`echo $fname |grep $1`" ]; then
continue; # masked, skip
elif verifyfile "$line"; then # already installed, skip
echo "$fname OK, skipping...";
continue;
fi
wget -c "$HOST$fname"; # download
if verifyfile "$line"; then # checksum
echo "$fname OK";
else # failed
echo "$fname has been corrupted during download, removing...";
let broken++;
rm -f $fname; # remove to allow download next time
fi
done
if [ $broken -ne 0 ]; then # status
echo "$broken maps failed to download, please rerun this script.";
else
echo "Installation successful.";
fi;
}
listmaps() {
if [ ! "$FILES" ]; then init; fi
for line in $FILES; do
line=`echo $line |sed 's/:/ /g'`;
fname=`getfname $line`;
if [ "$1" ] && [ ! "`echo $fname |grep $1`" ]; then
continue; # masked, skip
else
echo "$fname"; # print filename
fi
done
}
case "$1" in
verify)
verify;;
list)
listmaps "$2" |sort;;
license)
echo "This program is free software. It is available under the";
echo "terms of the GNU General Public License. If you did not";
echo "receive a copy of the GNU GPL with this program, you can";
echo "get on online at http://www.gnu.org/copyleft/gpl.html";;
help)
echo "$0 [install [PATTERN] | verify | list [PATTERN] | license | help]";
echo "PATTERN is any regular expression, default action is install";;
install|*)
instmaps "$2";;
esac
From the old thread...don't remember who made it.
This whole subject is really old though, there is a huge thread about it. Th