#!/bin/bash
#
# Quick hack to get apt-offline functionality automatized.
#
# Copyright © 2004  Guillem Jover <guillem@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#


setup()
{
  local discdir=$1

  cd $discdir
  arch=`dpkg --print-architecture`
  mkdir -p archives/partial lists/partial
  cp /etc/apt/sources.list .
  cp /var/lib/dpkg/status .
  if [ -f /var/lib/aptitude/pkgstates ]
  then
    cp /var/lib/aptitude/pkgstates .
  fi
  cat <<CONF > apt.conf.in
APT
{
  Architecture "$arch";
  Get::Download-Only "true";
};

Dir
{
  /* Use the disc for state information and redirect the status file from
     the /var/lib/dpkg default */
  State "%disc%";
  State::status "status";

  // Binary caches will be stored locally
  Cache::archives "%disc%/archives/";
  Cache "/tmp/";

  // Location of the source list.
  Etc "%disc%";
 };
CONF
}

gen_apt_conf()
{
  local discdir=$1

  sed -e "s:%disc%:$discdir:" < $discdir/apt.conf.in > $discdir/apt.conf
}

update()
{
  local discdir=$1

  gen_apt_conf $discdir

  export APT_CONFIG="$discdir/apt.conf"
  apt-get update
  apt-get dist-upgrade
}

upgrade()
{
  local discdir=$1

  gen_apt_conf $discdir

  export APT_CONFIG="$discdir/apt.conf"
  apt-get check
  apt-get --no-d -o dir::state::status=/var/lib/dpkg/status dist-upgrade
}

case $1
in
  help)
    cat <<HELP
apt-offline action discdir
action:
	setup			used on the local system
	update			used on the remote system
	upgrade|install		used on the local system
HELP
    ;;
  setup)
    setup $2
    ;;
  update)
    update $2
    ;;
  upgrade|install)
    upgrade $2
    ;;
esac

