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

# extract...
tmpfile=$(mktemp)
begin=`grep -n "\[Description]" $iif | cut -d":" -f1`
end=`grep -n "\[EndDescription]" $iif | cut -d":" -f1`
cat $iif | head -n $(($end-1)) | tail -n $(($end-$begin-1)) > $tmpfile
# ...edit...
echo "Please edit the description."
case "$EDITOR" in
vi|vim) $EDITOR $tmpfile;;
*)
  edited=0
  until [ "$edited" = "1" ]; do
	echo -n "Enter your favorite editor to edit the description: "
	read editor
	if [ -x $(which $editor) ]; then
	  $(which $editor) $tmpfile
	  edited=1
	elif [ -x $editor ]; then
	  $editor $tmpfile
	  edited=1
	fi
  done
;;
esac
# ..and merge
# i know, this is probably the most dirtiest way to replace a text between two codewords
# with another text, but i don't have any other idea yet
sed -i -e '/\[Description\]/,/\[EndDescription\]/d' $iif
echo '[Description]' >> $iif
cat $tmpfile >> $iif
echo '[EndDescription]' >> $iif
