#!/bin/sh
#
#	apt-walkabout tree
#
#	Create an apt-walkabout package cache in the current directory.
#
set -e

# abort if we are already in an instance
if [ -f bin/fetch ]; then
    echo "apt-walkabout: aborted, already in an apt-walkabout package cache"
    exit 1
fi

# check if current directory is empty
nf=`find -type f|wc --lines`
if [ $nf -ne 0 ]; then
    echo "apt-walkabout: warning," \
        "this directory has files, creating a new one below it"
    mkdir apt-walkabout
    cd apt-walkabout
fi

# create required directory structure
mkdir -p etc/apt
mkdir -p var/lib/apt/lists/partial
mkdir -p var/lib/dpkg
mkdir -p var/cache/apt/archives/partial

# copy the scripts
if [ -d /usr/lib/apt-walkabout ]; then
    mkdir bin
    cp --preserve=mode /usr/lib/apt-walkabout/* bin/
fi

# copy the documentation
if [ -d /usr/share/doc/apt-walkabout ]; then
    mkdir doc
    cp /usr/share/doc/apt-walkabout/* doc/
fi

# copy the source system's sources list
cp -i /etc/apt/sources.list etc/apt

# copy the source system's package list cache
cp `find /var/lib/apt/lists -maxdepth 1 -type f|grep -v lock\$` \
  var/lib/apt/lists/

# create our own lock file
touch var/lib/apt/lists/lock

echo "apt-walkabout: success, created instance in `pwd`"
exit 0
