#!/bin/sh

# Copyright (C) 2006-2008 Bart Martens <bartm@knars.be>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#URL32="http://www.openni.org/downloadfiles/opennimodules/openni-compliant-middleware-binaries/latest-unstable/112-primesense-nite-unstable-build-for-ubuntu-10-10-x86-32-bit-v1-3-1/download"
#URL64="http://www.openni.org/downloadfiles/opennimodules/openni-compliant-middleware-binaries/latest-unstable/112-primesense-nite-unstable-build-for-ubuntu-10-10-x86-64-bit-v1-3-1/download"
#URL32="http://www.openni.org/downloads/nite-bin-linux-x86-v1.5.2.21.tar.bz2"
#URL64="http://www.openni.org/downloads/nite-bin-linux-x64-v1.5.2.21.tar.bz2"
URL32=http://www.openni.org/wp-content/uploads/2012/12/NITE-Bin-Linux-x86-v1.5.2.21.tar.zip
URL32_SHA256SUM=8be3bf3b325ff6a3538381e8aacf2b7632a4dab422c1bd54ac7ee81d75a140e4
URL64=http://www.openni.org/wp-content/uploads/2012/12/NITE-Bin-Linux-x64-v1.5.2.21.tar.zip
URL64_SHA256SUM=57cd7daa38e82321270179574c3a96b9e8e92348b4c7011c8d58af3d1b2fdb45

downloadedfilename=primesense-nite.tar.bz2.zip
downloadedfile=/tmp/primesense-nite.tar.bz2.zip
cachedir=/var/cache/primesense-nite-nonfree
cachedfile=${cachedir}/$downloadedfilename

set -e

die_hard() {
	echo "ERROR: $1"
	echo "More information might be available at:"
	echo "  http://wiki.debian.org/PrimeSenseNite"
	exit 1
}

[ `whoami` = "root" ] || die_hard "must be root"

show_usage() {
	echo "Usage:"
	echo "  `basename $0` --install"
	echo "  `basename $0` --uninstall"
	echo "Additional options:"
	echo "  --verbose"
	echo "  --quiet"
	exit 1
}

basename=`basename $0`
getopt_temp=`getopt -o iufvq --long install,uninstall,fast,verbose,quiet -n ${basename} -- "$@"` || show_usage
eval set -- "$getopt_temp" || show_usage

ACTION=none
fast=no
verbose=no
quiet=no

while [ true ]
do
	case "$1" in
		-i|--install)
			ACTION="--install"
			shift
			;;
		-u|--uninstall)
			ACTION="--uninstall"
			shift
			;;
		-f|--fast)
			fast=yes
			shift
			;;
		-v|--verbose)
			verbose=yes
			shift
			;;
		-q|--quiet)
			quiet=yes
			shift
			;;
		--)
			shift
			break
			;;
		*)
			echo "Internal error!"
			exit 1
			;;
	esac
done

[ "$ACTION" != "none" -a $# -eq 0 ] || show_usage
[ "$quiet" != "yes" ] || verbose=no

[ "$verbose" != "yes" ] || echo "options : $getopt_temp"

UNPACKDIR=`mktemp -d /tmp/primesense-nite-nonfree.XXXXXXXXXX` || die_hard "mktemp failed"
echo "$UNPACKDIR" | grep -q "^/tmp/primesense-nite-nonfree\." || die_hard "paranoia"
cd "$UNPACKDIR" || die_hard "cd failed"

[ "$verbose" != "yes" ] || echo "temporary directory: $UNPACKDIR"

do_cleanup() {
	[ "$verbose" != "yes" ] || echo "cleaning up temporary directory $UNPACKDIR ..."
	cd /
	echo "$UNPACKDIR" | grep -q "^/tmp/primesense-nite-nonfree\." || die_hard "paranoia"
	rm -rf "$UNPACKDIR"
}

die_hard_with_a_cleanup() {
	do_cleanup
	die_hard "$1"
}

case "$ACTION" in

	--install)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		wgetquiet=' -q '
		wgetfast='-t 3 -T 15 '
		wgetalways=' -nd -P . '
		wgetprogress=' -v --progress=dot:default '
		
		wgetoptions="$wgetalways"
		[ "$fast" != "yes" ] || wgetoptions="$wgetoptions $wgetfast"
		if [ "$verbose" = "yes" ]; then
			wgetoptions="$wgetoptions $wgetprogress"
			echo "wgetoptions=$wgetoptions"
		else
			wgetoptions=" $wgetoptions $wgetquiet "
		fi

		if [ $(dpkg --print-architecture) = "amd64" ]; then
			downloadurl=${URL64}
			checksum=${URL64_SHA256SUM}
		else
			downloadurl=${URL32}
			checksum=${URL32_SHA256SUM}
		fi

		[ "$verbose" != "yes" ] || echo "downloading $downloadurl ..."
		wget $wgetoptions $downloadurl -O ${downloadedfile} \
			|| die_hard_with_a_cleanup "wget failed to download $downloadurl"

		echo "Checking integrity of downloaded file using sha256sum ..."
		checksum_file=${downloadedfile}.sha256
		echo "$checksum  $downloadedfile" > ${checksum_file}
		sha256sum -c ${checksum_file} || (echo checksum failed! try downloading again... ; exit 1)

		cp ${downloadedfile} ${cachedfile}

		[ "$verbose" != "yes" ] || echo "Creating .deb from $cachedfile ..."
		/usr/lib/primesense-nite-nonfree/primesense-nite-nonfree-make-deb ${cachedfile}

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"
		;;

	--uninstall)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"
		PKG="openni-modules-primesense-nite-nonfree"
		test dpkg -l ${PKG} 2> /dev/null > /dev/null && (
			echo -n "You have ${PKG} installed, this action won't "
			echo "uninstall anything you installed yourself."
			echo -n "Remove it manually if you don't want NITE anymore "
			echo "running as root:"
			echo
			echo "dpkg --purge ${PKG}"
		)

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"
		;;

	*)
		do_cleanup
		show_usage
		;;

esac

do_cleanup

[ "$verbose" != "yes" ] || echo "end of update-flashplugin-nonfree"

