#!/bin/sh
# this script unpacks and installs remote or local installation archive images created
# by the packCurrent.sh script.

if [ -z $1 ]; then
	echo "updateDXLaprs: missing architecture or local image file"
	echo "Usage: updateDXLaprs ARCH | file"
	echo "    Where ARCH may be: armv6, armv6tce, armv7hf, x86_32, x86_64, ..."
	echo ""
	echo "    If a local file is specified instead of one of the architectures above,"
	echo "    then this file will be used for performing the installation. The local"
	echo "    file must be a file that was created by the packCurrent.sh script."
	exit 1
fi

SOURCE=http://dxlaprs.hamspirit.at
FILE=dxlAPRS_$1-current

DST=$HOME/dxlAPRS
DSTMAP=$DST/aprsmap
DSTAPRS=$DST/aprs

# set up the destination directory structure
[ -d $DSTMAP ] || mkdir -p $DSTMAP
[ -d $DSTMAP/osm ] || mkdir -p $DSTMAP/osm
[ -d $DSTMAP/logs ] || mkdir -p $DSTMAP/logs
[ -d $DSTAPRS ] || mkdir -p $DSTAPRS

# retrieve the image
TMPDIR=$(mktemp -d)
if [ $? -ne 0 ]; then
	echo "Unable to create temporary directory."
	exit 1
fi

# if a file exists simply assume it's a local packCurrent image else try to download
if [ -f $1 ]; then
	FILE=$(basename $1 .tgz)
	cp -r $1 $TMPDIR
	cd $TMPDIR
else
	cd $TMPDIR
	wget $SOURCE/$FILE.tgz
	if [ $? -ne 0 ]; then
		echo "Unable to download image:" $SOURCE/$FILE.tgz
		rm -r $TMPDIR
		exit 1
	fi
fi

# if image file exists then unpack and move files to their destinations
if [ -r $FILE.tgz ]; then
	gunzip $FILE.tgz
	tar -xf $FILE.tar
	rm $FILE.tar
	echo "Installation directory:" $DST
	echo "Installing aprsmap-components..."
	mv -f aprsmap_common/poi.txt $DSTMAP/osm/
	mv -f aprsmap_common/* $DSTMAP/
	rm -r aprsmap_common
	mv -f aprsmap* $DSTMAP/
	echo "Installing aprs-components..."
	cp -r dxlAPRS_common/* $DSTAPRS/
	rm -r dxlAPRS_common
	cp -r scripts $DSTAPRS/
	rm -r scripts
	mv -f * $DSTAPRS/

# fixup file permissions and owners
	cd $DSTAPRS
	for f in $(ls); do
		test -x $f && sudo chmod 0755 $f
	done
	sudo chown root afskmodem
	sudo chmod +s afskmodem
	rm -r $TMPDIR
else
# either cp or wget failed above somehow to get an image into TMPDIR
	echo "Unable to locate temporary copy of image:" $FILE.tgz
	rm -r $TMPDIR
	exit 1
fi
