Yet another DJBDNS howto

#!/bin/bash
#
# WARNING: This script must run as root to be able to run. Do not run this script if
# you do not understand what that means.
#
# Howto install djbdns with a bunch of patches on Ubuntu (and possibly other systems)
# The following script downloads and installs a bunch of djbdns stuff and installs it,
# ready to run. Be careful with it. You might want just run each part manually instead
# of running the entire script. There's no error checking, so it's Russian Roulette.
#
# v20092311  Johnny Halfmoon
#

export BUILD_BASE="/usr/src/dns_test"
export INSTALL_BASE="/usr/local_test"
export SERVICE_BASE="/service_test"

# create installation folders in $INSTALL_BASE, for easy installation and removal
mkdir "$INSTALL_BASE"
cd "$INSTALL_BASE"
mkdir -p bin
mkdir -p djbdns-1.05
mkdir -p daemontools-0.76
mkdir -p ucspi-tcp-0.88
ln -s djbdns-1.05 djbdns
ln -s daemontools-0.76 daemontools
ln -s ucspi-tcp-0.88 ucspi-tcp

# get and unpack sources, if patch tarball exists, then assume everything is already downloaded
if [ -e "$BUILD_BASE/djbdns-patches-jhalfmoon.tar.gz" ] ; then
cd "$BUILD_BASE"
mkdir trash
mv * trash
mv trash/*.gz .
rm -rf trash
else
mkdir "$BUILD_BASE"
cd "$BUILD_BASE"
wget -c http://cr.yp.to/djbdns/djbdns-1.05.tar.gz
wget -c http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
wget -c http://cr.yp.to/ucspi-tcp/ucspi-tcp-0.88.tar.gz
wget -c http://milksnot.com/sites/snot/files/dumpdirdjbdns-patches-jhalfmoon.tar.gz
fi
ls *.gz | while read ding ; do tar xvzf "$ding" ; done

# compile and install daemontools
cd "$BUILD_BASE/admin/daemontools-0.76"
patch -p1 < ../../patches/daemontools-0.76.errno.patch
touch /etc/inittab
package/install
ls /command/ | while read ding ; do rm -f "$INSTALL_BASE/bin/$ding" ; done
mkdir "$INSTALL_BASE/daemontools/bin"
cp /command/* $INSTALL_BASE/daemontools/bin
rm -rf /command
find "$INSTALL_BASE/daemontools/bin/" -type f | while read ding ; do bla=`echo "$ding" | sed 's/^.*\///g'` ; ln -fs "$ding" "$INSTALL_BASE/bin/$bla" ; done
mv /service "$INSTALL_BASE/daemontools/"
ln -sf "$INSTALL_BASE/daemontools/service" $SERVICE_BASE

# compile and install ucspi-tcp
cd "$BUILD_BASE/ucspi-tcp-0.88"
patch -p1 < ../patches/ucspi-tcp-0.88.errno.patch
echo "$INSTALL_BASE/ucspi-tcp" > conf-home
make setup check
find "$INSTALL_BASE/ucspi-tcp/bin/" -type f | while read ding ; do bla=`echo "$ding"| sed 's/^.*\///g'` ; ln -fs "$ding" "$INSTALL_BASE/bin/$bla" ; done

# on ubuntu systems, /etc/inittab is no longer used, but /etc/event.d . Install svscan there.
cat > /etc/event.d/svscan <<EOF
# svscan - daemontools
start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5
stop on runlevel 0
stop on runlevel 1
stop on runlevel 6
respawn
exec "$INSTALL_BASE/daemontools/bin/svscanboot"
EOF
initctl start svscan

# patch, compile and install djbdns
cd "$BUILD_BASE"
folder=djbdns-patched ; rm "$folder" -rf ; cp -a djbdns-1.05 "$folder" ; cd "$folder" ; find ../patches/djbdns | grep .patch$ | sort | while read ding ; do echo "$ding" ========= ; patch < "$ding" | grep -i fail ; done
echo "$INSTALL_BASE/djbdns" > conf-home
make setup check
install -m 755 ../patches/dnsqrx "$INSTALL_BASE/djbdns/bin/"
install -m 755 ../patches/dnsroots-update "$INSTALL_BASE/djbdns/bin/"
find "$INSTALL_BASE/djbdns/bin/" -type f | while read ding ; do bla=`echo "$ding" | sed 's/^.*\///g'` ; ln -fs "$ding" "$INSTALL_BASE/bin/$bla" ; done
dnsroots-update > /etc/dnsroot.global

# configure and start dnscache, listening on 127.0.0.1
useradd -s /bin/false _dnscache
useradd -s /bin/false _dnslog
useradd -s /bin/false _tinydns
dnscache-conf _dnscache _dnslog "$SERVICE_BASE/dnscache" 127.0.0.1
echo 600:dump/data > "$SERVICE_BASE/dnscache/env/DUMPCACHE"

# increase the loglimits; log file size to 1MB and keep 50 files
cat > "$SERVICE_BASE/dnscache/main/log/run" << EOF
#!/bin/bash
exec setuidgid _dnslog multilog t s1000000 n50 ./main
EOF
killall -HUP multilog
svc -t "$SERVICE_BASE/dnscache"

#= THE END =