#!/bin/sh
# Copyright (C) 2008 Richard Levitte <richard@levitte.org>
#
# This program is made available under the GNU GPL version 2.0 or
# greater. See the accompanying file COPYING for details.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.

mtn_dir=.
mtn_keys=
shell_type=sh
if echo $SHELL | grep '/t?csh' > /dev/null; then shell_type=csh; fi
values_only=false

usage () {
    echo "mtnopt: Usage: mtnopt [-c] [-s] [-d dir] [-k keys] [-v] [-h]"
}
while [ ! $# = 0 ]; do
    case $1 in
	-c)
	    shell_type=csh
	    ;;
	-s)
	    shell_type=sh
	    ;;
	-d*)
	    mtn_dir=`echo "$1" | sed -e 's/^-d//'`
	    if [ -z "$mtn_dir" ]; then shift; mtn_dir="$1"; fi
	    if [ -z "$mtn_dir" ]; then
		echo "mtnopt: missing required argument for -d" >&2
		usage
		exit 1
	    fi
	    ;;
	-k*)
	    mtn_keys=`echo "$1" | sed -e 's/^-k//'`
	    if [ -z "$mtn_keys" ]; then shift; mtn_keys="$1"; fi
	    if [ -z "$mtn_keys" ]; then
		echo "mtnopt: missing required argument for -k" >&2
		usage
		exit 1
	    fi
	    ;;
	-v)
	    values_only=true
	    ;;
	-h|--help)
	    usage
	    exit 0
	    ;;
	--version)
	    echo "mtnopt from monotone 1.1"
	    exit 0
	    ;;
    esac
    shift
done

if [ ! -d "$mtn_dir" ]; then
    echo "mtnopt: $mtn_dir isn't a directory or is missing"
    exit 1
elif [ ! -d "$mtn_dir/_MTN" ]; then
    echo "mtnopt: $mtn_dir isn't a monotone workspace"
    exit 1
elif [ ! -f "$mtn_dir/_MTN/options" ]; then
    echo "mtnopt: $mtn_dir/_MTN/options isn't a file or is missing"
    exit 1
fi

cat $mtn_dir/_MTN/options | while read L; do
    eval `echo "$L" | sed -e 's/^ *\([a-z][a-z]*\) \(.*\)$/key=\1; val=\2/'`
    if [ -z "$mtn_keys" ] || echo "$key" | egrep "^$mtn_keys\$" > /dev/null; then
	if $values_only; then
	    echo "$val"
	else
	    if [ "$shell_type" = sh ]; then
		echo "MTN_$key=\"$val\";"
	    else
		echo "set MTN_$key=\"$val\";"
	    fi
	fi
    fi
done
