#!/bin/sh
refsys=$1
listname=$2

if [ -z "$refsys" -o -z "$listname" ]
then
	echo Usage: $0 '<hostname>' '<list name>'
	exit 1
fi

tmpfile=/tmp/packagelist-init.$$
if [ -z "$EDITOR" ]
then
	EDITOR=vi
fi

trap "rm $tmpfile" 0

echo
echo
echo Fetching package list from $refsys with ssh...
ssh $refsys sh >$tmpfile <<EOF
[ -f /etc/debian_version ] && 
	dpkg-query --show -f '\${Package} \${Version} \${Architecture}\n' ||
 	rpm -qa --qf '%{Name} %{Version}-%{Release} %{Arch}\n' | sed s/\(none\)/noarch/
EOF
echo
echo Got $(wc -l $tmpfile | cut -f1 -d" ") packages.
echo
echo You now can edit the list of packages which will be
echo imported into your package list. Press Enter to open the
echo list with your standard editor. Delete the lines with the
echo packages you don\'t want to import, then save the file and exit.
read line
$EDITOR $tmpfile
echo
echo Got $(wc -l $tmpfile | cut -f1 -d" ") packages.
echo
echo Press Enter to import the packages into package list \"$listname\", or '^C' to abort.
read line

packagemanager csvimport $listname <$tmpfile
