#! /bin/sh
#
# Copyright 1999-2012 University of Chicago
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

. ${GLOBUS_LOCATION:-/usr}/share/globus/globus-script-initializer

checker="${sbindir}/globus-rvf-check"

usage()
{
    echo "Usage: $(basename $0) -s|-l LRM|-f PATH"
}

while getopts "vhsl:f:" i; do
    case $i in
        s)
            path="${sysconfdir}/globus/gram/job-manager.rvf"
            ;;
        l)
            path="${sysconfdir}/globus/gram/${OPTARG}.rvf"
            ;;
        f)
            path="$OPTARG"
            ;;
        h)
            usage
            exit 0
            ;;
        v)
            verbose=1
            ;;
        *)
            usage
            exit 1
            ;;
    esac
            
done

if [ -z "$path" ]; then
    usage
    exit 1
fi

if [ ! -x "$checker" ]; then
    echo "Configuration error: Missing $checker" 1>&2
    exit 1
fi

if [ ! -w "$(dirname $path)" ]; then
    echo "Permission denied: unable to write to $(dirname $path)" 1>&2
    exit 1
fi

cleanup_tmp()
{
    if [ -n "$tmpdir" ] && [ -d "$tmpdir" ]; then
        rm -rf "$tmpdir"
    fi
}

trap "cleanup_tmp" 0

tmpdir="$(mktemp -d -t globus-rvf-edit.XXXXXX)"
if [ $? != 0 ]; then
    tmpdir="${TMPDIR-/tmp}/globus-rvf-edit.$$"
    mkdir "$tmpdir"
    if [ $? != 0 ]; then
        echo "Unable to create temporary directory" 1>&2
        exit 1
    fi
fi

tmpfile="${tmpdir}/rvf.temp"

if [ -r $path ]; then
    cp "$path" "${tmpfile}"
else
    cat <<EOF > "${tmpfile}"
#
# $(basename $path)
#
# RVF Record Format consists of the following without the leading #
# Records are separated by blank lines
#
# Attribute: RSL-attribute-name
# Description: "Description string" # string describing the attribute, within
#                                   # quotes, " must be escaped as \"
# Default: "default value"          # default value is an RSL value string
# DefaultWhen: when-value           # space-delimited sequence of zero or more
#                                   # of
#                                   # GLOBUS_GRAM_JOB_SUBMIT
#                                   # GLOBUS_GRAM_JOB_MANAGER_RESTART
#                                   # GLOBUS_GRAM_JOB_MANAGER_STDIO_UPDATE
# ValidWhen: when-value             # same as DefaultWhen values
# RequiredWhen: when-value          # same as DefaultWhen values
# Values:                           # space-delimited sequence of legal values
#                                   # for the rsl attribute (e.g. "yes no"
# Publish:                          # valid values true or false, indicating
#                                   # whether
#                                   # scripts/create-rsl-documentation.pl will 
#                                   # generate doc for this attribute
# 
EOF
fi

while true; do
    ${VISUAL:-vi} "${tmpfile}"
    if [ $? != 0 ]; then
        echo "Error editing temp file" 1>&2
        exit 1
    fi

    "${checker}" ${vebose:+"-d"} "${tmpfile}"
    if [ $? != 0 ]; then
        printf "Edit again [yn]: "
        read answer
        if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
            exit 1
        fi
    else
        break
    fi
done

if [ -f "$path.bak" ]; then
    rm -f "$path.bak" 
fi
if [ -f "$path" ]; then
    mv "$path" "$path.bak"
fi
mv "$tmpfile" "$path"
