#!/bin/sh
#
#	apt-walkabout adopt-via-ssh
#
#	Copies the list of installed packages to your apt-walkabout
#	instance, along with a courtesy copy of source list.
#
#	Run this on a system other than the target.
#	Run this when you have network connectivity to the target.
#	Run this before trying to "apt-walkabout fetch".
#
set -e

name=${1}
if [ -z "${name}" ]; then
    def=`hostname`
    echo -n "apt-walkabout system name [${def}] ? "
    read name
    if [ -z "${name}" ]; then
        name=${def}
    fi
fi

# prompt for the remote system username and host name
host=${2}
if [ -z "${host}" ]; then
    def=${USER}@server
    echo -n "username and system name, in user@system format [${def}] ? "
    read host
    if [ -z "${host}" ]; then
        host=${def}
    fi
fi

if [ ! -f bin/adopt ]; then
    echo "aborted, we're not in an apt-walkabout instance"
    exit
fi

mkdir -p var/lib/dpkg
mkdir -p etc/apt

HERE=/tmp/apt-walkabout.$$
mkdir -p ${HERE}
scp -C ${host}:/var/lib/dpkg/status ${host}:/etc/apt/sources.list ${HERE}
gzip --best --stdout ${HERE}/status > var/lib/dpkg/status.${name}.gz
gzip --best --stdout ${HERE}/sources.list > etc/apt/sources.list.${name}.gz
ls -l var/lib/dpkg/status.${name}.gz etc/apt/sources.list.${name}.gz
rm -rf ${HERE}
