#!/bin/bash
###############################################################################
##                                                                           ##
##        Script for generating a local Ubuntu mirror and DVD images         ##
##                         containing all of Ubuntu                          ##
##                                                                           ##
##                  2006 by Robert Entner - www.entner.net                   ##
##                                                                           ##
## http://blog.entner.net/2006/08/16/make-ubuntu-dvds-including-everything/  ##
##                                                                           ##
## 2006-08-16  initial version                                               ##
## 2006-08-23  usability changes                                             ##
##                                                                           ##
###############################################################################

## the used mirror server (defaults to archive.ubuntu.com and ubuntu/)
#HOSTURL=gd.tuwien.ac.at
#HOSTPATH=opsys/linux/ubuntu/archive/

## if you want to add your own packages to the last DVD add the path to a
## directory containing your .debs here
#EXTRAPACKAGES=/home/entner/entner2/burts-deb-repository

## directory where the ISOs are stored (defaults to local directory)
#ISODIR=/home/entner/entner2/linux-isos

###############################################################################
##      it should not be necessary to change anything beyond this line       ##
###############################################################################

# defaults
if [ -z "${ISODIR}" ]; then
    ISODIR=.
fi
if [ -z "${HOSTURL}" ]; then
    HOSTURL=archive.ubuntu.com
fi
if [ -z "${HOSTPATH}" ]; then
    HOSTPATH=ubuntu/
fi

# check some dependencies
if ! `which ruby >/dev/null`; then 
    echo "Please install ruby (sudo apt-get install ruby)"
    exit
fi
if ! `which mkisofs >/dev/null`; then 
    echo "Please install mkisofs (sudo apt-get install mkisofs)"
    exit
fi
if ! `which debmirror >/dev/null`; then 
    echo "Please install debmirror (sudo apt-get install debmirror)"
    exit
fi
if ! `which debpartial >/dev/null`; then 
    echo "Please install debpartial (sudo apt-get install debpartial)"
    exit
fi
if [[ "$EXTRAPACKAGES" != "" ]]; then
    if ! `which dpkg-scanpackages >/dev/null`; then 
	echo "Please install dpkg-scanpackages (sudo apt-get install dpkg-dev)"
	exit
    fi
fi

# the following line mirrors:
# universe and multiverse from dapper, dapper-security, and dapper-updates
mkdir -p ubuntu
debmirror --nosource -m --passive --host=${HOSTURL} \
    --root=${HOSTPATH} --method=ftp --progress \
    --dist=dapper,dapper-security,dapper-updates \
    --section=main,restricted,universe,multiverse \
    --arch=i386 ubuntu/ --ignore-release-gpg

# create package descriptors for DVD sized directories
mkdir -p ubuntu-dvds
rm -rf ubuntu-dvds/*
debpartial --nosource --dirprefix=ubuntu \
    --section=main,restricted,universe,multiverse \
    --dist=dapper,dapper-security,dapper-updates \
    --size=DVD ubuntu/ ubuntu-dvds/

# generate file symlinks to DVD directories 
# (debcopy has to be extracted from package debpartial)
if [ ! -e debcopy ]; then
    cp /usr/share/doc/debpartial/examples/debcopy.gz .
    gunzip debcopy.gz
fi
ruby debcopy -l ubuntu/ ubuntu-dvds/ubuntu0
ruby debcopy -l ubuntu/ ubuntu-dvds/ubuntu1
ruby debcopy -l ubuntu/ ubuntu-dvds/ubuntu2
ruby debcopy -l ubuntu/ ubuntu-dvds/ubuntu3

# generate ISO images
for i in 0 1 2 3; do 
    echo -e "Use Applications->Accessories->Terminal and enter\nsudo apt-cdrom add\nfor each DVD" > ubuntu-dvds/ubuntu$i/README
done
mkisofs -f -J -r -V "Ubuntu 6.06.x $(date -I) 1/4" -o $ISODIR/ubuntu-6.06.x-$(date -I)-complete-i386-dvd1.iso ubuntu-dvds/ubuntu0
mkisofs -f -J -r -V "Ubuntu 6.06.x $(date -I) 2/4" -o $ISODIR/ubuntu-6.06.x-$(date -I)-complete-i386-dvd2.iso ubuntu-dvds/ubuntu1
mkisofs -f -J -r -V "Ubuntu 6.06.x $(date -I) 3/4" -o $ISODIR/ubuntu-6.06.x-$(date -I)-complete-i386-dvd3.iso ubuntu-dvds/ubuntu2
mkisofs -f -J -r -V "Ubuntu 6.06.x $(date -I) 4/4" -o $ISODIR/ubuntu-6.06.x-$(date -I)-complete-i386-dvd4.iso ubuntu-dvds/ubuntu3
# this is used to fill the remaining space on the last disk with useful packages
if [[ "$EXTRAPACKAGES" != "" ]]; then
    ln -s $EXTRAPACKAGES ubuntu-dvds/ubuntu3/extras
    dpkg-scanpackages $EXTRAPACKAGES /dev/null > $EXTRAPACKAGES/Packages
    mkisofs -f -J -r -V "Ubuntu 6.06.x $(date -I) 4b/4" -o $ISODIR/ubuntu-6.06.x-$(date -I)-complete-i386-plus-extras-dvd4.iso ubuntu-dvds/ubuntu3
    rm ubuntu-dvds/ubuntu3/extras
fi
