#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/edit - ask user for edit
#
# Copyright (c) 2003-2004 Frank Meyer <frank@eisfair(dot)org>
#
# Creation:     2003-07-19  fm
# Last Update:  $Id$
#
# usage: edit [-apply|-apply-stopstart] file
#        edit [-apply|-apply-stopstart] package message
#
# return values:
#   0  editor started
#   1  editor not started
#
# This program 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 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

# include function library
. /var/install/bin/config_shlib

# include configuration
. /etc/config.d/setup

apply=''
if [ "$1" = "-apply" ]
then
    apply='restart'
    shift
elif [ "$1" = "-apply-stopstart" ]
then
    apply='stopstart'
    shift
fi

if [ $# -eq 1 ]
then
    package=`basename $1`
    configdir=`dirname $1`
else
    package="$1"
    configdir='/etc/config.d'

    clrhome
    mecho -info "$2"
    mecho
fi

# if EDITOR is set, try to add path
[ "$EDITOR" = "" ] && EDITOR='joe'
[ "$EDITOR_FALLBACK" = "" ] && EDITOR_FALLBACK='joe'

editor="`echo "$EDITOR_FALLBACK" | cut -d' ' -f1`"
editor_param="`echo "$EDITOR_FALLBACK" | cut -d' ' -f2-`"
[ "$editor" = "$editor_param" ] && editor_param=''

if [ "$editor" != "" ] && [ ! -x "$editor" ]
then
    if [ -x "/var/install/bin/$editor" ]
    then
        EDITOR_FALLBACK="/var/install/bin/$editor $editor_param"
    elif which $editor >/dev/null 2>/dev/null
    then
        EDITOR_FALLBACK="`which $editor` $editor_param"
    else
        EDITOR_FALLBACK="`which joe`"
    fi
fi

editor="`echo "$EDITOR" | cut -d' ' -f1`"
editor_param="`echo "$EDITOR" | cut -d' ' -f2-`"
[ "$editor" = "$editor_param" ] && editor_param=''

if [ "$editor" != "" ] && [ ! -x "$editor" ]
then
    if [ -x "/var/install/bin/$editor" ]
    then
        EDITOR="/var/install/bin/$editor $editor_param"
    elif which $editor >/dev/null 2>/dev/null
    then
        EDITOR="`which $editor` $editor_param"
    else
        EDITOR="$EDITOR_FALLBACK"
    fi
fi


exitval=0

# edit first package...
current_package=`echo $package | sed -e 's/ .*$//g'`

if [ -f $configdir/$current_package ]
then
    editor="$EDITOR_FALLBACK"
    param=''

    editor_pgm=`echo $EDITOR | cut -d' ' -f1`
    dirname=`dirname $editor_pgm`
    if  [ "$dirname" = "/var/install/bin" ]
    then
        if $EDITOR --check $configdir/$current_package
        then
            editor="$EDITOR"
        else
            mecho
            anykey

            mecho
            mecho "The configuration of this package can't be opened by the config"
            mecho "editor currently setup on your system. This may be due to some"
            mecho "inconsistent config files or options."
            mecho "You can now edit the configuration manually using editor $editor."
            mecho
        fi
    else
        editor="$EDITOR"
        mecho
        mecho "There is no configuration editor for the EIS/FAIR console configured"
        mecho "on your system. Alternatively you can edit the configuration manually"
        mecho "using editor $editor."
        mecho
    fi

    editor_pgm=`echo $editor | cut -d' ' -f1`
    basename=`basename $editor_pgm`
    if [ "$basename" = "joe" ]
    then
        mecho "You can get help by joe if you press 'Ctrl-K H'."
        mecho
        param="-nobackups"
    elif [ "$basename" = "vi" -o "$basename" = "vim" ]
    then
        mecho "You can get help by vi/vim if you enter ':help'."
        mecho
    fi

    dirname=`dirname $editor_pgm`
    if [ "$dirname" != "/var/install/bin" ]
    then
        if ! /var/install/bin/ask "Continue" "yes"
        then
            exit 1
        fi
    fi

    old_umask=`umask`
    umask 077       # do not allow anybody to read anything
                    # config or logfile may contain secret passwords

    cp -p $configdir/$current_package /tmp/edit-$$

    while [ 1 ]
    do
        $editor $param $configdir/$current_package

        if [ -f /usr/local/bin/eischk -a -f /etc/check.d/$current_package ]
        then
            mecho -info "Checking configuration file ..."
            mecho
            if [ "$current_package" = "environment" ]
            then
                eischk_weak='--weak'
            else
                eischk_weak=''
            fi

            if ! /usr/local/bin/eischk $eischk_weak                          \
                                       --config /etc/config.d                \
                                       --check  /etc/check.d                 \
                                       --log    /tmp/$current_package.log    \
                                       --package $current_package
            then
                if ! /var/install/bin/ask "Edit configuration again" "yes"
                then
                    exitval=1
                    break
                fi
            else
                break
            fi
        else
            break
        fi
    done

    umask $old_umask

    if ! diff /tmp/edit-$$ $configdir/$current_package >/dev/null
    then
        backup_config $current_package /tmp/edit-$$
    fi
    rm -f /tmp/edit-$$
    rm -f /tmp/$current_package.log
fi



# call apply-script if exists
apply_script ()
{
    if [ -f /var/install/config.d/$1.sh ]
    then
        sh /var/install/config.d/$1.sh
    else
        return 0
    fi
}

# call init-script if exists
init_script ()
{
    if [ -f /etc/init.d/$1 ]
    then
        sh /etc/init.d/$1 $2
    fi
}

# can start package
can_start ()
{
    # read configuration file
    . /etc/config.d/$1
    startvar=`echo START_$1 | tr 'a-z' 'A-Z'`
    eval start='$'$startvar
    test "$start" = "yes"
}

if [ $exitval -eq 0 -a "$apply" != "" ]
then
    mecho
    if [ "$package" = "base" -o "$package" = "environment" ]
    then
        if  apply_script $package
        then
            init_script $package restart
        fi
    elif /var/install/bin/ask "Activate configuration now" "yes"
    then
        # activate changes
        for current_package in $package
        do
            if [ "$apply" = "restart" ]
            then
                if  apply_script $current_package &&
                    can_start    $current_package
                then
                    # restart package
                    init_script $current_package restart
                else
                    # stop package
                    init_script $current_package stop
                fi
            elif [ "$apply" = "stopstart" ]
            then
                init_script $current_package stop
                if  apply_script $current_package &&
                    can_start    $current_package
                then
                    init_script    $current_package start
                fi
            fi
        done
    fi
    anykey
fi

exit $exitval