#!/bin/bash
#
#Copyright 2012 - 2017 CurlyMo & mkreisl <development@xbian.org>
#
#This file is part of XBian - XBMC on the Raspberry Pi.
#
#XBian is free software: you can redistribute it and/or modify it under the
#terms of the GNU General Public License as published by the Free Software
#Foundation, either version 3 of the License, or (at your option) any later
#version.
#
#XBian 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 XBian. If not, see <http://www.gnu.org/licenses/>

#|------------------------------------|
#|          Global variables          |
#|------------------------------------|

# The arguments this module accepts
ARGUMENTS=(select insert delete update);

#|------------------------------------|
#|          Include files             |
#|------------------------------------|

source $BASEPATH/modules/licensempg2/functions
if [ $GUIMODE -eq 1 ]; then
	source $BASEPATH/modules/licensempg2/dialogs
fi
source $BASEPATH/functions/config

#|------------------------------------|
#|           Main program             |
#|------------------------------------|

# Executes the GUI version of this module
function showGUIFn() {
	getCurrentLicenseMPG2Fn
	CURRENTLICENSE=$LICENSE;
	showLicenseMPG2InputDlg $CURRENTLICENSE;
	CODE=$?
	if [ $CODE -eq 0 ]; then
		if [ ! -z $RETURN ] && [ ! -z $CURRENTLICENSE ]; then
			updateLicenseMPG2Fn $RETURN $CURRENTLICENSE
			if [ $? -eq 1 ]; then
				showLicenseMPG2UpdatedDlg;
				askForRebootFn;
			elif [ $? -eq 0 ]; then
				showErrorDlg;
			else
				showInvalidLicenseFormatDlg;
			fi
		elif [ ! -z $RETURN ] && [ -z $CURRENTLICENSE ]; then
			insertLicenseMPG2Fn $RETURN;
			if [ $? -eq 1 ]; then
				showLicenseMPG2InsertedDlg;
				askForRebootFn;
			elif [ $? -eq 0 ]; then
				showErrorDlg;
			else
				showInvalidLicenseFormatDlg;
			fi
		elif [ -z $RETURN ] && [ -z $CURRENTLICENSE ]; then
			return 1;
		else
			deleteLicenseMPG2Fn $CURRENTLICENSE;
			if [ $? -eq 1 ]; then
				showLicenseMPG2DeletedDlg;
				askForRebootFn;
			else
				showErrorDlg;
			fi
		fi
	fi
}

# Executes the command line version of this module
#  $1 Argument [String]
#  $2 License [String]
# Returns
#  *Select [String]
#  - License
#  *Update [Integer]
#  - 1: Success
#  - 0: Failed
#  - -1: License incorrect format
function showCMDFn() {
	case $1 in
		"select")
			getCurrentLicenseMPG2Fn;
			echo $LICENSE;
		;;
		"update")
			shift;
			if [ $# -eq 1 ]; then
				getCurrentLicenseMPG2Fn
				updateLicenseMPG2Fn $1 $LICENSE;
				RETURN=$?;
				if [ $RETURN -eq 1 ]; then
					echo 1;
				elif [ $RETURN -eq 2 ]; then
					echo -1;
				else
					echo 0;
				fi
			else
				echo 0;
			fi
		;;
		"insert")
			shift;
			if [ $# -eq 1 ]; then
				insertLicenseMPG2Fn $1;
				RETURN=$?;
				if [ $RETURN -eq 1 ]; then
					echo 1;
				elif [ $RETURN -eq 2 ]; then
					echo -1;
				else
					echo 0;
				fi
			else
				echo 0;
			fi
		;;		
		"delete")
			getCurrentLicenseMPG2Fn
			deleteLicenseMPG2Fn $LICENSE;
			echo $?;
		;;
	esac
}