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

# t to
# n name
# p silent

. /usr/local/lib/muglin/muglin.d/libmuglin
while getopts t:n:p OPTION; do
  case "$OPTION" in
    t)
	FILE="$OPTARG"
    ;;
    n)
	checkimagename "$OPTARG"
	NAME="$OPTARG"
    ;;
    p)
	PHP=1;
    ;;
    \?)
	    echo "Unknown argument: \"$1\". Aborting!"
	    exit -1
    ;;
    :)
	    echo "Option \"$OPTARG\" requires an argument. Aborting!"
	    exit -1
    ;;
  esac
done

if [ "$NAME" = "" ]; then
  echo "Missing \"-n <image name>\". Aborting"
  exit -1;
fi
if [ "$FILE" = "" ];then
  echo "Missing \"-t <file>\". Aborting"
  exit -1;
fi

. /etc/muglin/base.conf

IPATH=`echo "SELECT Path FROM Images WHERE Name = '$NAME'" | mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB --skip-column-names -B`
if [ ! -d "$IPATH" ]; then
  echo "Could not find path for \"$NAME\". Aborting"
  exit -2
fi

OUTPUT=`readlink -n -f $FILE`
if [ "$OUTPUT" = "" ]; then
  echo "Directory \"`dirname $(readlink -n -m $FILE)`\" does not exist. Aborting"
  exit -3
fi
cd $IPATH
echo ">Exporting $IPATH to $OUTPUT..."
if [ -e "$OUTPUT" ]; then
  echo -n ">>File \"$OUTPUT\" already exists. Overwrite? (y/N)[n]"
  read overwrite
  if [ "$overwrite" = "y" ]; then
  #two step delete because i think its faster - srdja
    echo "" > $OUTPUT
    rm $OUTPUT
  else
    echo "Canceled by user."
    exit
  fi
fi
zip -r $OUTPUT * > /dev/null 2>&1
zipret=$?
cd $OLDPWD
if [ "$zipret" = "0" ]; then
  echo ">Exported"
else
  echo ">Failed to export image (zip said $zipret)"
  exit $zipret
fi
