#!/bin/bash
#
# src-get	wrapper for apt-get to allow easy user download of
#		sources and packages from multiple distributions
#
#               Copyright (c) 2007 by Emmet Hikory <emmet.hikory@gmail.com>
#               Distributed under the GPL version 2
#
# Version 0.0.1+alpha1
#
#
#   The configuration directory stores the master configuration for the
# system.  For single-user systems, the current values may be sufficient,
# but multi-user systems may want to use a publically available $CONF.
#
#   Wherever located, the directory should contain files as follows:
#
# conf.skel should be as below (without comments)
#
# APT::Get::Download-Only "true";
# Dir::State "/home/USER/.src-get/state/";
# Dir::Cache "/home/USER/.src-get/cache/";
# Dir::Etc "/home/USER/.src-get/etc/";
# Dir::Parts "/home/USER/.src-get/parts/";
# Dir::Bin "/home/USER/.src-get/bin/";
#
# distribution.list (for any desired distrbutions)
#
# This is a standard sources.list file for the distribution named
# If the case statements below do not match the set of files, one of
# the two should be updated.
#

CONF=/home/$USER/etc/src-get/

set -e

mkskel()
{
  mkdir -p $HOME/.src-get/etc $HOME/.src-get/state/lists/partial
  mkdir -p $HOME/.src-get/cache/archives/partial $HOME/.src-get/parts
  mkdir -p $HOME/.src-get/bin/
}

case "$1" in
  new)
    rm -rf /.src-get/*
    case "$2" in
      oldstable|stable|testing|unstable)
        mkskel && cp $CONF/$2.list $HOME/.src-get/etc/sources.list && \
        sed s/USER/$USER/ < $CONF/conf.skel > $HOME/.src-get/apt.conf && \
        apt-get -c $HOME/.src-get/apt.conf update 2>&- | grep -v dpkg
        ;;
      breezy|dapper|edgy|feisty)
        mkskel && cp $CONF/$2.list $HOME/.src-get/etc/sources.list && \
        sed s/USER/$USER/ < $CONF/conf.skel > $HOME/.src-get/apt.conf && \
        apt-get -c $HOME/.src-get/apt.conf update 2>&- | grep -v dpkg
        ;;
      *)
        mkskel && cp $2 $HOME/.src-get/etc && \
        sed s/USER/$USER/ < $CONF/conf.skel > $HOME/.src-get/apt.conf && \
        apt-get -c $HOME/.src-get/apt.conf update 2>&- | grep -v dpkg
        ;;
    esac
    ;;
  source)
    apt-get -c $HOME/.src-get/apt.conf source $2
    ;;
  download)
    echo "This doesn't work today.  Patches welcome." >&2
    ;;
  clean)
    rm -rf $HOME/.src-get/*
    ;;
  *)
    echo "Usage: $0 new distibution | file" >&2
    echo "       $0 source src-pkg" >&2
    echo "       $0 download bin-pkg" >&2
    echo "       $0 clean" >&2
    exit 1
esac
 
exit 0
