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

case $1 in
	-help|--help|-h|help|"")
	echo "Cowardly imports an image.
Usage: muglin import_quiet FILE IMG_PATH IIF
FILE: Image to import
IIF: Path to image.iif"
	exit;;
esac


FILE=$1
IIF=$3
IMG_PATH=$2

if [ ! -e "$IIF" ]; then echo "\"$IIF\" is missing or not a file!"; exit; fi
if [ ! -e "$FILE" ]; then echo "\"$FILE\" is missing or not a file!"; exit; fi
if [ "$IMG_PATH" = "" ]; then echo "\"$IMG_PATH\" is not provided!"; exit; fi
if [ -d "$IMG_PATH" ]; then echo "\"$IMG_PATH\" already exists!";exit; fi

echo -n ">Creating \"$IMG_PATH\" and extracting \"$FILE\"... "
mkdir -p $IMG_PATH
output=`readlink -f $FILE`
cd $IMG_PATH
unzip "$output" > /dev/null 2>&1
ret=$?
if [ $ret = "0" ]; then
	echo done
else
	echo "Error extracting \"$FILE\"! (unzip said: $ret)"
	exit $ret
fi

file=$IIF
PHP=1 . /usr/local/lib/muglin/show_iif > /dev/null 2>&1

#case $ITYPE in
#	file) TYPE=1;;
#	dd) TYPE=0;;
#	*) echo "Unknown type in IIF! Aborting!" exit -3;;
#esac

#echo -n "Verifying image..."
#echo "done"

echo -n ">Commiting to DB..."
mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB -B -e"INSERT INTO Images (Name,Path,Architectur,Comment,Enabled,Type) VALUE ('$INAME','$IMG_PATH','$IARCH','$ICOMMENT',0,$ITYPE);" > /dev/null 2>&1
ret=$?
case $ret in
  0) echo "done";;
  *) echo "Error, mysql said: $ret"; exit $ret;

