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

. /usr/local/lib/muglin/muglin.d/libmuglin
if [ "$1" != "-name" ]; then
  echo "\"-name <image name>\" is missing! Aborting!"
  exit -1
fi
if [ "$2" = "" ]; then
  echo "Missing argument for \"-name\". Aborting!"
  exit -1
fi
checkimagename "$2"

. /etc/muglin/base.conf

IPATH=`echo "SELECT Path FROM Images WHERE Name = '$2'" | mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB --skip-column-names -B`
if [ "$IPATH" = "" ]; then
  echo "Could not find path for image. May be not registred?"
  exit -2
fi
if [ ! -d "$IPATH" ]; then
  echo "Path \"$IPATH\" is not a directory, aborting!"
  exit -3
fi
if [ "`echo $IPATH | grep -e ^$IMG_PATH`" = "" ]; then
  echo "\"$IPATH\" is not a path in the image storage directory \"$IMG_PATH\". Aborting!"
  exit -4
fi
echo "DELETE FROM Images WHERE Name = '$2'" | mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB --skip-column-names -B
rm $IPATH -rf 
