#!/bin/bash

debug=1

[ "$HOSTNAME" = "MyHostNameGoesHere" ] || exit 0

myname=`dd if=/dev/vda skip=4190208 count=1`
static=`dd if=/dev/vda skip=4190209 count=1`
privat=`dd if=/dev/vda skip=4190210 count=1`

[ "$debug" -gt 0 ] && echo "Taking down interfaces"

ifdown eth0
ifdown eth1
ifdown eth2

[ "$debug" -gt 0 ] && echo "Setting hostname"

if [ -n "$myname" ]
then
  echo $myname >/etc/hostname
  sed -i "s/MyHostNameGoesHere/$myname/" /etc/hosts
  hostname $myname
  export HOSTNAME=$myname
  sed -i "s/MyHostNameGoesHere/$myname/g" `grep -lr MyHostNameGoesHere /etc`
fi

[ "$debug" -gt 0 ] && echo "Checking for static IP"

if [ -n "$static" ]
then
  sed -i "s/^domain clients./domain /" /etc/resolv.conf
  mydomain=`grep ^domain /etc/resolv.conf | cut -d' ' -f2`
  sed -i "s/.*$myname$/$static\t$myname\t$myname.$mydomain/" /etc/hosts
  sed -i "s/iface eth0 inet dhcp/iface eth0 inet manual/" /etc/network/interfaces
  sed -i "s#iface eth1 inet manual#iface eth1 inet static\n\taddress $static/24\n\tgateway\t${static%.*}.5#" /etc/network/interfaces
fi

[ "$debug" -gt 0 ] && echo "Checking for private IP"

if [ -n "$privat" ]
then
  sed -i "s#iface eth2 inet manual#iface eth2 inet static\n\taddress $privat#" /etc/network/interfaces
fi

[ "$debug" -gt 0 ] && echo "Bringing up interfaces where appropriate"

[ -z "$static" ] && ifup eth0
[ -n "$static" ] && ifup eth1
[ -n "$privat" ] && ifup eth2

[ "$debug" -gt 0 ] && echo "Email aliases"

if ! [ -s /etc/mail/aliases ]
then
  cd /etc/mail
  cat <<EOF > aliases
root: antony
antony: $myname@Haelentor.de
EOF
  make
  /etc/init.d/sendmail reload
fi

[ "$debug" -gt 0 ] && echo "Do the SSH server"

dpkg-reconfigure openssh-server
sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/; s/PubkeyAuthentication yes/PubkeyAuthentication yes\nPubkeyAcceptedKeyTypes=+ssh-dss/" /etc/ssh/sshd_config
sed -i "s/HashKnownHosts yes/HashKnownHosts no/" /etc/ssh/ssh_config

[ "$debug" -gt 0 ] && echo "Sort out the log files"

sed -i "s/MyHostNameGoesHere/$myname/g" `find /var/log -type f`

[ "$debug" -gt 0 ] && echo "Resize the root partition"

resize2fs -p /dev/vda1

[ "$debug" -gt 0 ] && echo "Do the crontabs"

min=$(($RANDOM%11+1))
sed -i "s/HMIN/$min/" /etc/crontab
hour=$(($RANDOM%5+1))
min=$(($min+12))
sed -i "s/DHOUR/$hour/; s/DMIN/$min/" /etc/crontab
hour=$(($hour+1))
min=$(($min+12))
sed -i "s/WHOUR/$hour/; s/WMIN/$min/" /etc/crontab
hour=$(($hour+1))
min=$(($min+12))
sed -i "s/MHOUR/$hour/; s/MMIN/$min/" /etc/crontab
hour=$(($hour+1))
min=$(($min+12))
sed -i "s/YHOUR/$hour/; s/YMIN/$min/" /etc/crontab

[ "$debug" -gt 0 ] && echo "Fix inittab"
sed -i "s/getty 38400/getty --noclear 38400/" /etc/inittab

[ "$debug" -gt 0 ] && echo "Restart syslog"

/etc/init.d/rsyslog restart

[ "$debug" -gt 0 ] && echo "Restart SSH"

/etc/init.d/ssh restart

[ "$debug" -gt 0 ] && echo "Update & upgrade"

aptitude update
aptitude -y upgrade

[ "$debug" -gt 0 ] && echo "Don't run this again"

sed -i '/firstboot/d' /etc/rc.local

[ "$debug" -gt 0 ] && echo "Destroy the evidence"

rm -f $0

