#!/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/>.       #
########################################################################

. /etc/muglin/base.conf

SHOW_IIF='/usr/local/lib/muglin/show_iif'

# -f from
# -p php mode
# -h help

while getopts phf: OPTION; do
	case $OPTION in
		f)			
			if [ -e $OPTARG ]; then export FILE="$(readlink -f "$OPTARG")"
			else echo "\"$OPTARG\" is not a file, exiting"; exit
			fi
		;;
		p) export PHP=1 ;;
		h)
			echo "Import an image (and commit it)
        Options:
                -f <File>    File to import from
                -h	     This help"
			exit
		;;
		
		\?)
		  echo "Unknown option \"-$1\". Aborting"
		  exit -1
		;;
		:)
		  echo "Option \"-$OPTARG\" requires an argument. Aborting."
		  exit -1
		;;

	esac
	shift
done

# image.tgz
# image.iif
# partitions

decho(){
	if [ "$PHP" = "" ]; then echo $@;fi
}

if [ "$FILE" = "" ]; then echo "No import file given, exiting!"; exit; fi


decho ">Importing $FILE"
decho "-n >>Extracting image.iif..."
cd $IMG_PATH/
# this -o is kind of brutal, but no image.iif has lost something there, so why not
unzip -o $FILE image.iif > /dev/null 2>&1

# WTF, why did i wrote $# instead of $? oO -- srdja
if [ "$?" = 0 ]; then decho "done";
else echo "Error extracting image.iif, exiting!";exit -1; fi

decho ">>Parsing \"image.iif\"... "

file="$IMG_PATH/image.iif"

. $SHOW_IIF
checkimagename "$INAME"
TPATH=`echo $INAME | sed -e "s/ /_/g"`

INDB=`echo "SELECT Name FROM Images WHERE Name = '$INAME'" | mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB --skip-column-names -B`
if [ "$INDB" != "" ]; then
  echo "A image named \"$INDB\" is already in our database, aborting!"
  exit -3
fi

decho -n ">>Import $NAME? ([y]es/[n]o)?"
if [ "$PHP" = "" ]; then
  read import
fi

if [ "$import" != 'y' -o "$PHP" = "1" ]; then
	decho -n ">>Canceling..."
	rm "$IMG_PATH/image.iif"
	decho "done"
	exit
fi

/usr/local/lib/muglin/quiet_import "$FILE" "$IMG_PATH/$TPATH" "$IMG_PATH/image.iif"
rm "$IMG_PATH/image.iif"
decho "Importing finished"



