#!/bin/bash
# $Id: make_aptmirror,v 1.14 2009-02-07 08:04:14 tim Exp $

DIST=${1}
TMPDIR1=tmp-build1

rm -fr ${TMPDIR1}
rm -fr aptmirror
mkdir ${TMPDIR1}

cd ${TMPDIR1}
restore -r -f ../install.dmp
cd ..

echo "deb ftp://einstein/debian ${DIST} main" >${TMPDIR1}/etc/apt/sources.list
echo "deb ftp://einstein/debian-security ${DIST}/updates main" >>${TMPDIR1}/etc/apt/sources.list
echo "nameserver 192.168.100.100" >${TMPDIR1}/etc/resolv.conf
echo "search home.woodall.me.uk" >>${TMPDIR1}/etc/resolv.conf

# APT_REGEX[*] is the regex for selecting the kernels and packages we
# want available for installing
#
# APTPKGS is a list of all packages we either want installed or
# available for install with the exception of APT_REGEX packages. Note
# that we explicitly remove any packages from this list that are already
# installed in the minimal system.

APT_REGEX[0]='linux-image-2\\.6-.*'
APT_REGEX[1]='aufs-modules-2\\.6-.*'
APT_REGEX[2]='atl2-modules-2\\.6-.*'

APTPKGS="
  adduser
  apt-utils
  ash
  bind9
  bsdmainutils
  busybox
  console-common
  console-data
  console-tools
  cpio
  cramfsprogs
  cron
  dash
  debconf
  debconf-i18n
  dhcp3-client
  dialog
  dselect
  dump
  fetchmail
  gawk
  gettext-base
  gnupg
  groff-base
  grub
  guile-1.6-libs
  host
  ifupdown
  initramfs-tools
  iptables
  iputils-ping
  klibc-utils
  klogd
  kpartx
  less
  libcompress-zlib-perl
  libconsole
  libdb1-compat
  libdigest-sha1-perl
  libedit2
  libgdbm3
  libgsasl7
  libguile-ltdl-1
  libhtml-parser-perl
  libhtml-tagset-perl
  libhtml-tree-perl
  libidn11
  libio-zlib-perl
  libklibc
  libkrb53
  liblocale-gettext-perl
  liblockfile1
  libltdl3
  libmailutils1
  libmysqlclient15off
  libncursesw5
  libpopt0
  libqthreads-12
  libsocket6-perl
  libssl0.9.8
  libstdc++5
  libtasn1-3-bin
  libtext-charwidth-perl
  libtext-iconv-perl
  libtext-wrapi18n-perl
  liburi-perl
  libvolume-id0
  libwrap0
  libwww-perl
  locales
  logrotate
  lvm2
  lynx
  m4
  mailutils-pop3d
  mailx
  make
  mbr
  mdadm
  module-init-tools
  mysql-common
  net-tools
  netbase
  netkit-ping
  ntp
  nvi
  openbsd-inetd
  openssh-client
  openssh-server
  openssl
  passwd
  perl
  perl-modules
  procmail
  procps
  psmisc
  rmail
  rsync
  sendmail
  sendmail-base
  sendmail-bin
  sendmail-cf
  sensible-mda
  spamassassin
  spamc
  squid
  squid-common
  ssh
  sysklogd
  syslinux
  tcpd
  telnet
  udev
  update-inetd
  vim
  vim-common
  vim-runtime
  "

for pkg in ${APTPKGS}; do
  if [ ! -f ${TMPDIR1}/var/lib/dpkg/info/${pkg}.list ]; then
    GETPKGS="${GETPKGS} ${pkg}"
  fi
done

chroot ${TMPDIR1} /bin/bash <<CHROOT

export LANG=C

#install apt-move so we can build a mirror
apt-get update
apt-get autoremove
apt-get -y install apt-move

#Clean the cache, download everything we want to install and then build
#the mirror
apt-get clean
apt-get -y -d --reinstall install ${GETPKGS} ${APT_REGEX[@]}
apt-move get
apt-move move
apt-move packages

CHROOT

mv ${TMPDIR1}/mirrors aptmirror
rm -fr ${TMPDIR1}

exit 0

