#!/bin/sh

curdir=`dirname "$0"`
absolute=`expr "$0" : '/.*'`
if [ $absolute = 0 ];then
  curdir=`pwd`/"$curdir"
fi

rt_mode_file="${curdir}/../default.mode"

if [ $# -eq 0 ] ; then
    if [ -r "$rt_mode_file" ] ; then
	mode=`cat "$rt_mode_file"`
    else
	mode="enterprise"
    fi
    echo "Visual Mainwin default run-time mode is: '$mode'"
    exit 0
fi

if [ "$1" = "-s" ] ; then
    silent=1
    shift
else
    silent=0
fi

case "$1" in
'-c')
    mode="classic"
    ;;
'-e')
    mode="enterprise"
    ;;
'-p')
    if [ "$2" != "" ];
    then mode=professional:$2
    else mode=professional
    fi
    ;;
*)
    echo "USAGE: mwset_rtmode [ [-s] {-e|-c|-p [<keyword>]} ]" 1>&2
    echo "When used with no parameters: display the current default mode." 1>&2
    echo "-s : Silent, set the modewith out prompting for confirmation." 1>&2
    echo "-e : Set the mode to enterprise." 1>&2
    echo "-c : Set the mode to classic." 1>&2
    echo "-p : Set the mode to professional." 1>&2
    echo "<keyword> : Specify for product separation." 1>&2
    exit 2
    ;;
esac

if [ $silent -ne 1 ]; then
	echo "Changing the default run-time mode will affect all applications that use this Visual MainWin installation. "
	echo "Apply change? (y/[n])"
 	if [ "$MWOS" = "linux" ]; then
 	    read ans
 	else
	    ans=`line`
 	fi
	if [ "$ans" != "y" -a "$ans" != "Y" ]; then
	    echo "Operation cancelled."
	    exit 1
	fi
fi
       
if [ -w "$rt_mode_file" ] ; then
    created_file=0
else 
    created_file=1
fi
	
(echo $mode > "$rt_mode_file") > /dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo "Failed: You lack permissions to change the default Mainwin run-time mode." 1>&2
	exit 2
fi

if [ $created_file -eq 1 ]; then
    if [ `/bin/uname` = "SunOS" ];
	then id=/usr/xpg4/bin/id
	else id=id
    fi
    if [ `$id -u -n` = "root" ]; then
	/bin/chgrp 0 "$rt_mode_file"
    fi
    /bin/chmod 0644 "$rt_mode_file"
fi

echo "Visual Mainwin default run-time mode set to: '$mode'."
exit 0
