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

if [[ "$1" == "-p" ]];then
	shift;
	PHP=1;
fi
if [[ "$file" != "" ]];then
  FILE=$file
else
  FILE=$1
  shift
fi

if [[ ! -e $FILE ]]; then
  echo "File $FILE does not exist, aborting!"
  exit -1
fi

if [[ "$(grep \[ChangeLog\] $FILE)" == "" ]]; then
  echo "Missing changelog begin in $FILE. Aborting!"
  exit -2
fi

if [[ "$PHP" = "1" ]]; then
	USER=$1
	shift
	entry=$(echo $(date +%D\,\ %T): ${USER}@$(hostname): $@)
else
	entry=$(echo $(date +%D\,\ %T): $(whoami)@$(hostname): $@)
fi
echo "Adding entry: $entry"
count=$(grep -n \[EndChangeLog\] $FILE)
if [[ "$count" == "" ]]; then
  #some stupid forgot to end the changelog
  echo "$entry" >> $FILE
  echo '[EndChangeLog]' >> $FILE
else
  newent=$(echo $entry | sed -e 's/\//\\\//g')
  sed -i -e "s/\[EndChangeLog\]/$newent\n\[EndChangeLog\]/g" $FILE
fi
