#!/bin/bash

########################################################################
# MuGLIn - MuGLIn GNU/Linux Installation		                       #
#                                                                      #
# Copyright (C) 2010 Jakob Gurnhofer <jakob.gurnhofer@gmail.com>       #
# Copyricht (C) 2010 Srdjan Markovic <smark2ki@htl.moedling.at>        #
#                                                                      #
# This file is part of MuGLIn source code.                             #
#                                                                      #
# MuGLIn 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.                                  #
#                                                                      #
# MuGLIn 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 MuGLIn. If not, see <http://www.gnu.org/licenses/>.       #
########################################################################

# Path to the apps...
curl="/usr/bin/curl -k"

PATH=/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/muglin
export PATH
###########################################################################
# UI-Stuff
# For now it echos, later use may be direct upload to server or sth other

# 0...woman-like,1...quieter,2...more quiet,etc
VERBOSE=1
export VERBOSE
# same as VERBOSE
LOG=0
export LOG

# if set to anything but "", i will log inputs to log
# WARNING: PASSWORDS WILL BE LOGGED TOO!!!!!
LOGINPUT=1

# Quite self-explaining, syntax: log <text>
log(){
       :
}

# Same too
display(){
	echo $@
}

# By now, we just have a simple cli, but later, we may use sth else
# Syntax: output <priority> <options/text>
# Just a notice: i (srdjan) am using output 1 -n <message> in muglin for echoing without a newline.
# may and will not work everywhere :)
output(){
	case $1 in
		l) shift; log $@;;
		d) shift; display $@;;
				
		*)
			local priority=$1
			shift
			if [ "$priority" -gt "$VERBOSE" ]; then
				display $@
			fi
			if [ "$priority" -gt "$LOG" ]; then
				log $@
			fi
		;;
	esac
}

# the same problem, the same solution :)
input(){
    read $@ input
    if [ "$LOGINPUT" != "" ]; then
	log $input
    fi
    echo $input
}

###########################################################################
# Now the error-handling.
# Most of the errors may be solved by trying again
# But some of them are not recoverable and should abort the installation
# So if we are registered, we need to unregister
# And again, all this stuff is experimental, so lets have a chance to find the error
# (I know, there are ctrl+f2 etc ^^)

# Syntax: die <message-text> <log-text>
# if not empty, $2 will be used instead of $1 in log
die(){
	echo "ERROR: $1"
	if [ "$2" = "" ]; then
	  log $1
	else
	  log $2
	fi
	if [ "$ON_ERROR" = "failback-user" ]; then
	  echo "Escaping to shell. Press enter to continue"
	  read
	  /bin/bash
	  return
	elif [ "$ON_ERROR" = "abort" ]; then
	# TODO: use variable instad of fixed username and pwd
	  $curl -F reason="error" -F errormsg="$@" http://${SERVER}:666/api/unregister.php -u muglinclient:1x2v3l4c.
	  echo "Abortig"
	  exit
	fi
}

