#!/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
. /usr/local/lib/muglin/muglin.d/libmuglin

$LIBDIR="/usr/local/lib/muglin/"
verify(){
  topic=$1
  shift
  if [[ "$1" == "" ]]; then
	echo "Missing argument for -$topic, aborting!"
	exit -1
  fi
  if [[ "$(echo $1 | egrep '^-')" != "" ]];then
    echo "Your entry starts with an \"-\"."
    echo "Are you sure there are no arguments missing? (y/n)"
    read sure
    if [[ "$sure" != "y" ]]; then
      echo "Canceled by user"
      exit
    fi
  fi
}

# n name
# c comment
# d desc
# a chlog

while getopts n:c:a:d OPTION; do
  case "$OPTION" in
    n)
      checkimagename "$OPTARG"
      NAME=$OPTARG;
      path=$(mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB --skip-column-names -B -e"SELECT Path FROM Images WHERE Name = '$OPTARG';")
      if [[ ! -d $path ]]; then
	echo "\"$path\" is not a directory. Maybe wrong image name? Aborting!"
	exit -2
      fi
      iif=${path}/image.iif
    ;;
    c)
      verify comment $1
      COMMENT="$OPTARG"
      cmds="$cmds cmt"
    ;;
    d)
      cmds="$cmds dsc"
    ;;
    a)
      verify addchlog $1 
      CHLOG="$OPTARG";
      cmds="$cmds chl";;
    \?)
      echo "Unknown option \"-$1\". Aborting"
      exit -1
    ;;
    :)
      echo "Option \"-$OPTARG\" requires an argument. Aborting."
      exit -1
    ;;
  esac
done

if [[ "$NAME" == "" ]]; then
	echo "Missing -name! Aborting";
	exit -1
fi

for cmd in $cmds; do
  case $cmd in
    chl) $LIBDIR/add_chlog_entry $iif $CHLOG;;
    cmt)
      oldComment=$(grep "Comment=" $iif | sed -e 's/\//\\\//g')
      newComment=$(echo $COMMENT | sed -e 's/\//\\\//g')
      sed -i -e "s/$oldComment/Comment=$newComment/g"
      mysql -u$MYS_USER -p"$MYS_PASS" -d$MYS_DB -B -e"UPDATE Images SET Comment='$COMMENT' WHERE Name = '$NAME';"
      echo "Comment updated"
    ;;
    dsc)
        . /usr/local/lib/muglin/edit_desc
    ;;
  esac
done
