#!/bin/bash

if [ ! "$UID" = "0" ]
then
   echo "This script requires being run as root, eg: \$ sudo $0"
   exit
fi

if [ ! `crontab -l | grep "apt-mirror" -c` = "0" ]
then
   echo "Bailing out early. It appears that apt-mirror is already install and run by cron"
   echo "Perhaps you have already installed this before"
   exit
fi

if [ -d /var/www/ubuntu ]
then
   echo "Bailing out early. It appears that /var/www/ubuntu already exists"
   echo "Perhaps you have already installed this before"
   exit
fi

echo -e "`crontab -l`\n`echo '2 */12 * * * /usr/bin/apt-mirror'`" | crontab -

apt-get install apt-mirror apache2

if [ ! -d /var/www ]
then
   echo "Bailing out early. It appears that /var/www doesnt exist"
   echo "Perhaps there is a problem with your apache2 installation"
   exit
fi

mkdir -p /home/apt-mirror/ubuntu
chown -R apt-mirror:apt-mirror /home/apt-mirror
ln -s /home/apt-mirror/ubuntu /var/www/ubuntu

mv /etc/apt/mirror.list /etc/apt/mirror.list.orig
cat > /etc/apt/mirror.list << EOF
set nthreads 50
set _tilde 0
set base_path /home/apt-mirror/ubuntu
deb-i386 http://archive.ubuntu.com/ubuntu intrepid main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu intrepid-updates main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu intrepid-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu intrepid main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu intrepid-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu intrepid-security main restricted universe multiverse
clean http://archive.ubuntu.com/ubuntu
EOF

echo
echo If you want to use this repsoitory, edit the file /etc/apt/sources.list and add lines like follows:
echo
echo deb http://localhost/ubuntu/mirror/archive.ubuntu.com/ubuntu intrepid main restricted universe multiverse
echo deb-src http://localhost/ubuntu/mirror/archive.ubuntu.com/ubuntu intrepid main restricted universe multiverse
echo deb http://localhost/ubuntu/mirror/archive.ubuntu.com/ubuntu intrepid-updates main restricted universe multiverse
echo deb-src http://localhost/ubuntu/mirror/archive.ubuntu.com/ubuntu intrepid-updates main restricted universe multiverse
echo deb http://localhost/ubuntu/mirror/archive.ubuntu.com/ubuntu intrepid-security main restricted universe multiverse
echo deb-src http://localhost/ubuntu/mirror/archive.ubuntu.com/ubuntu intrepid-security main restricted universe multiverse
echo
echo Modify the host name to suit for client machines other than the local machine
echo

apt-mirror


