#!/bin/bash
#######################################################################
# #
# Automatically upgrade/downgrade the LDMud-driver #
# #
# Written by Andre "Guandalug la'Fay" Meiske #
# #
#######################################################################
# Some global settings
#
# The trailing slash is required for all directory-settings
# Where shall the diff-files be stored?
DIFFDIR=$HOME/src/ldmud-src/
# Where is the driver-source to be found
SRCDIR=$HOME/src/ldmud-cur/
# Where in our Mudlib are the manpages?
MUDDOCDIR=${HOME}/mudlib/doc/man/
# Parameters needed for 'wget' to work properly
WGETPARAMS="-q"
# Name of the setting used to configure the driver
SETTINGNAME=forgottendreams
# Driver-Branch dependent settings:
# a) Development-branch (3.3)
# Where are the patches to be found?
DIFFSOURCE="http://www.bearnip.com/ftp/mud/ldmud-33/"
# what's the prefix/suffix of the diff-files?
# diff-file-name is build as prefix-<oldver>-<newver>.suffix
DIFFPREFIX="diff-33-"
DIFFSUFFIX=".gz"
# b) Current branch (3.2)
# # Where are the patches to be found?
# DIFFSOURCE="http://www.bearnip.com/ftp/mud/ldmud-32/"
# # what's the prefix/suffix of the diff-files?
# # diff-file-name is build as prefix-<oldver>-<newver>.suffix
# DIFFPREFIX="diff-32dev"
# DIFFSUFFIX=".gz"
###################################################################
#
# The script itself
#
# Find the driver's current version and calculate the name of the
# diff-file we need. in 'reverse' mode we need (current-1)-(current),
# and (current)-(current+1) otherwise
#
function get_cur_version()
{
CURRENT=`fgrep "Project-Version" ${SRCDIR}lpmud.prj |\
cut -d' ' -f4 | cut -d')' -f1`
if [ "X$1" == "Xreverse" ]; then
OLD_VER=$((CURRENT-1))
NEW_VER=$CURRENT
else
OLD_VER=$CURRENT
NEW_VER=$((CURRENT+1))
fi
DIFF=${DIFFPREFIX}${OLD_VER}-${NEW_VER}${DIFFSUFFIX}
}
#
# Check if we already have the patch. Try downloading it via 'wget'
# if not.
#
# Check again if we have the patch. If not, bail out, otherwise
# apply it. Afterwards copy all changed manpages to the mudlib
# directory, so your mud-internal manuall access system can find them
#
# Unfortunately manpages won't get deleted this way, so every now and then
# you need check for (re)moved manpages
#
function apply_patch()
{
# Get the newest patch
if [ ! -e ${DIFFDIR}${DIFF} ]; then
cd ${DIFFDIR}
wget ${WGETPARAMS} ${DIFFSOURCE}${DIFF}
fi
# Apply it
if [ -e ${DIFFDIR}${DIFF} ]; then
cd ${SRCDIR}
zcat ${DIFFDIR}${DIFF} | patch $1 > /dev/null
# Copy new manpages and other stuff to the mud-directory
cd ${SRCDIR}doc/
for file in `find . -mtime -1 -type f`; do
cp $file ${MUDDOCDIR}$file > /dev/null
done
return 1
fi
return 2
}
# Main function:
if [ "x$1" == "x-help" ]; then
echo 'Usage: patch-o-matic [-reverse] [-build|-install]'
exit 0
fi
if [ "X$1" == "X-reverse" ]; then
# if '-reverse' is given, downgrade the driver one version
# (useful for debugging)
shift;
get_cur_version "reverse"
apply_patch "-R -p1"
if [ $?==1 ]; then
echo "LDMud backpatched to version $OLD_VER"
else
echo "Missing patch, version $CURRENT unchanged"
fi
else
# otherwise repeat attempt to upgrade the driver until
# we don't find any new patches any more
CHANGED=0
while (true); do
get_cur_version "forward"
apply_patch "-p1"
if [ $? -eq 2 ]; then
if [ $CHANGED -eq 1 ]; then
echo "LDMud patched to version $CURRENT"
else
echo "No patch available, version $CURRENT is unchanged."
fi
break
else
CHANGED=1
fi
done
fi
# if '-build' was given on the commandline, change into the 'real'
# sourcedirectory, clean up, apply the given settings and rebuild
# both driver and utils
if [ "X$1" == "X-build" ]; then
# build the new driver
cd ${SRCDIR}src
make distclean
${SRCDIR}src/settings/${SETTINGNAME}
make ldmud utils
fi
# if '-install' was given on the commandline, change into the 'real'
# sourcedirectory, clean up, apply the given settings, rebuild
# both driver and utils and install them directly
if [ "X$1" == "X-install" ]; then
# build the new driver
cd ${SRCDIR}src
make distclean
${SRCDIR}src/settings/${SETTINGNAME}
make ldmud utils install install-utils
fi