#!/bin/sh

BNAME=${0##*/}

emu=$1
case "$emu" in
	"doscam"|"mgcamd")
		tarchive="cccam_keys.tar.gz"
	;;
	*)
		echo "[$BNAME] Usage: $0 {doscam|mgcamd}"
		exit 1
	;;
esac

workdir=${tarchive%%.*}

cleanup() {
	rm -rf /tmp/$workdir /tmp/$tarchive
}

cleanup

cd /tmp && wget -q http://dark-network.eu/downloads/DNA-Updater/keys/$tarchive

if [ -e $tarchive ]; then
	mkdir $workdir
	cd $workdir
	tar -xzf /tmp/$tarchive

	test -d /var/keys || mkdir -p /var/keys

	cp -f $(find var/keys -type f) /var/keys/
	chown 0:0 /var/keys/*
	chmod u+w /var/keys/*

	echo "$BNAME: Key-Update erfolgreich abgeschlossen"
else
	echo "$BNAME: Fehler beim Download von $tarchive"
fi

cleanup
