#!/bin/sh

# apt-off - scripts for off-line apt-get
# Copyright (C) 2002 Theppitak Karoonboonyanan <thep@linux.thai.net>
# 
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Usage:
# - either set APTROOT env to your media's mount point or cd to it
#   before issuing the commands
# * set up your media to represent your local machine
#     $ apt-off set
# * bring your media to remote machine (with fast network) to suck files
#     $ apt-off suck update
#     $ apt-off suck dist-upgrade
#     $ apt-off suck install ...
#       or whatever (as if you called apt-get itself)
# * spread the files from your media to your local machine
#     # apt-off spread dist-upgrade
#     # apt-off spread install ...
#       or whatever (as if you called apt-get itself)

if test "x$APTROOT" = "x"; then
  APTROOT=`pwd`
fi

SOURCELIST=$APTROOT/sources.list
STATUS=$APTROOT/status
ARCHIVESDIR=$APTROOT/archives
LISTSDIR=$APTROOT/lists

# status overriding
if [ "$1" = "-s" ]; then
  shift
  if echo "$1" | grep "^/" >/dev/null; then
    STATUS=$1
  else
    STATUS=$APTROOT/$1
  fi
  shift
fi

case $1 in
  set)
    if [ ! -d $ARCHIVESDIR/partial ]; then
      mkdir -p $ARCHIVESDIR/partial
    fi
    if [ ! -d $LISTSDIR/partial ]; then
      mkdir -p $LISTSDIR/partial
    fi
    if [ ! -f $SOURCELIST ]; then
      cp /etc/apt/sources.list $SOURCELIST
    fi
    cp /var/lib/dpkg/status $STATUS
    ;;
  suck)
    shift
    exec apt-get \
      -o Dir::Etc::SourceList=$SOURCELIST \
      -o Dir::Cache="$APTROOT" \
      -o Dir::Cache::Archives=$ARCHIVESDIR \
      -o Dir::State=$APTROOT \
      -o Dir::State::Status=$STATUS \
      -o Dir::State::Lists=$LISTSDIR \
      -d $*
    ;;
  spread)
    shift
    exec apt-get \
      -o Dir::Etc::SourceList=$SOURCELIST \
      -o Dir::Cache::Archives=$ARCHIVESDIR \
      -o Dir::State::Lists=$LISTSDIR \
      $*
    ;;
  *)
    exit 1
    ;;
esac

exit 0

