#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: show_value,v 1.9 2005/04/11 14:14:34 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: 
#
# ALICE
# Automatic Linux Installation and Configuration Environment
# 
##############################################################################
#
# Copyright (c) 2000 SuSE Linux Solutions AG, Eschborn, Germany
# All rights reserved.
#
# This file is part of ALICE, which was sponsored by
# T-Online International AG
#
# Author: Fabian Herschel
# 
##############################################################################
#
# show_value fqhn type tag_name
# shows the value of a tag
#
##############################################################################
#
# Changes:
#     - Now you can call show_value with:
#       show_value hostname section tag_name
#
# NOTES:
#
usage()
{
. $alice_dir/lib/alicerc
   echo "usage:"
   echo "  $_self -h | --help | -?"
   echo "  $_self [options]         full-host-name  config_section [ tag_name ]"
   echo "  $_self [options] --dhcp  dhcp-class-name config_section [ tag_name ]"
   echo "  options are:"
   echo "    --list-tagnames"
   echo "    --optional"
   echo "    --tcf-style"
   echo "    --value-only"
   echo "    --debug"
   
}

print_value()
{
	typeset tag_name=$1
	eval tag_value="\$$tag_name"
	if [ $tcfout -eq 0 ]
	then
		if [ ! "$silent" ]; then
			echo 'Value of tag ' $tag_name ' (enclosed in >>> and <<<):'
			echo ">>>$tag_value<<<"
		else 
			echo "$tag_value"
		fi
	else
		echo "<$tag_name>$tag_value</$tag_name>"
	fi
}

params=""

if [ -z "$alice_dir" ]; then
	alice_dir=$(cd $(dirname $0)/..; pwd)
	export alice_dir
fi

tag_type=""
tag_name=""

list_only=0
tcfout=0

while [ $# -gt 0 ]
do
   case $1 in
       -h | --help | -? | --?) 
             usage
                          exit 1
                          ;;
       -fqhn | --fqhn ) export fqhn=$2; shift # only to be backwards compatible
            ;;
       --optional ) params="$params --optional"
                    ;;
       --debug ) params="$params --debug"
                    ;;
       --dhcp )
                 dhcp_class=$2
                 dhcp="--dhcp"
                 shift
                     ;;      
		--value-only ) silent=1
			;;
		--list-tagnames ) list_only=1  # only allowed without tag-name set
			;;
		--tcf-style ) tcfout=1 # print in tag style
			;;

       -*) 
             echo "Unknown option $1"
             usage
             exit 1
             ;;
			
       * )
             if [ -z "$fqhn" ]
             then
                 fqhn=$1
             else
				 if [ -z "$tag_type" ]
				 then
					 tag_type=$1
				 else
					 tag_name=$1
				 fi
             fi
             ;;
   esac
   shift
done

. $alice_dir/lib/alicerc
if [ ! "$silent" ]; then WELCOME; fi
GET_CONFIGURATION $dhcp $params $tag_type
if [ -n "$tag_name" ]
then
	print_value $tag_name
else
	if [ $list_only -eq 1 ]
	then
		echo "$ALICE_TAGS_FOUND"
	else
		for tag_name in $ALICE_TAGS_FOUND
		do
			print_value $tag_name 
		done
	fi
fi

if [ ! "$silent" ]; then GOOD_BYE; fi
