#!/bin/bash
TEMP="/tmp/mpkgcache"
REPO="https://public.pionica.com/pubfiles/linux/slackware64-15"

if [ "x$1" = "x" ]; then
	echo "Usage: mpkg packagename"
	exit 1
fi

if [ ! -d "$TEMP" ]; then
	mkdir "$TEMP"
fi

cd "$TEMP"

#if [ ! -f list ]; then
	rm -f list list.md5
	wget "$REPO/list"
	wget "$REPO/list.md5"
#fi

if ! md5sum --quiet -c list.md5; then
	echo "Wrong md5sum of list"
	exit 1
fi

FILENAME=`grep -G "^$1 " list | cut -d " " -f 2`

if [ "x$FILENAME" = "x" ]; then
	echo "No such package"
	exit 1
fi

rm -f "$FILENAME" "$FILENAME.md5"
wget "$REPO/$FILENAME"
wget "$REPO/$FILENAME.md5"

if md5sum --quiet -c "$FILENAME.md5"; then
	if ! ls /var/log/packages/"$1"* 2>/dev/null 1>&2 ; then
		installpkg "$FILENAME";
	else
		upgradepkg "$FILENAME";
	fi;
else
	echo "Wrong md5sum of $FILENAME"
	exit 1
fi

