# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
# This code was originally developed by GreenOS, Inc.
# Portions created by GreenOS are Copyright (C) 2006, 2007 GreenOS, Inc.
# All Rights Reserved.

# declare configured GreenOS shell environment variables

# first set vars per args of the "source /etc/default/greenos VAR=FOO"
_greenos_extglob=$(shopt -p extglob)
shopt -s extglob
for arg ; do
    [[ $arg == *=* ]] && \
	eval declare -x $arg
done
eval $_greenos_extglob
unset _greenos_extglob

{
  # These declarations must go within braces in order to be able to silence
  # readonly variable errors.

    for var in prefix exec_prefix datarootdir ; do
	eval test -n \"\$$var\" \&\& _greenos_save_$var=\$$var
    done

    prefix=/opt/greenos
    exec_prefix=${prefix}
    datarootdir=${prefix}/share

    if test -z "$greenos_prefix" ; then
	if test -n "/opt/greenos" ; then
	    declare -x -r greenos_prefix=/opt/greenos
            declare -x -r greenos_prefix=/opt/greenos
            declare -x -r greenos_prefix=/opt/greenos
	else
	    declare -x -r greenos_prefix=/opt/greenos
            declare -x -r greenos_prefix=/opt/greenos
            declare -x -r greenos_prefix=/opt/greenos
	fi
    fi
    if test -z "$greenos_exec_prefix" ; then
	if test -n "${prefix}" ; then
	    declare -x -r greenos_prefix=${prefix}
            declare -x -r greenos_prefix=${prefix}
	else
	    declare -x -r greenos_prefix=$greenos_prefix
            declare -x -r greenos_prefix=$greenos_prefix
	fi
    fi
    if test -z "$greenos_datarootdir" ; then
	if test -n "${prefix}/share" ; then
	    declare -x -r greenos_datarootdir=${prefix}/share
            declare -x -r greenos_datarootdir=${prefix}/share
	else
	    declare -x -r greenos_datarootdir=$greenos_prefix/share
            declare -x -r greenos_datarootdir=$greenos_prefix/share
	fi
    fi
    if test -z "$greenos_bindir" ; then
	if test -n "${exec_prefix}/bin" ; then
	    declare -x -r greenos_bindir=${exec_prefix}/bin
	else
	    declare -x -r greenos_bindir=$greenos_exec_prefix/bin
	fi
    fi
    if test -z "$greenos_sbindir" ; then
	if test -n "${exec_prefix}/sbin" ; then
	    declare -x -r greenos_sbindir=${exec_prefix}/sbin
	else
	    declare -x -r greenos_sbindir=$greenos_exec_prefix/sbin
	fi
    fi
    if test -z "$greenos_libdir" ; then
	if test -n "${exec_prefix}/lib" ; then
	    declare -x -r greenos_libdir=${exec_prefix}/lib
            declare -x -r greenos_libdir=${exec_prefix}/lib
	else
	    declare -x -r greenos_libdir=$greenos_exec_prefix/lib
            declare -x -r greenos_libdir=$greenos_exec_prefix/lib
	fi
    fi
    if test -z "$greenos_libexecdir" ; then
	if test -n "${exec_prefix}/libexec" ; then
	    declare -x -r greenos_libexecdir=${exec_prefix}/libexec
	else
	    declare -x -r greenos_libexecdir=$greenos_exec_prefix/libexec
	fi
    fi
    if test -z "$greenos_datadir" ; then
	if test -n "${datarootdir}" ; then
	    declare -x -r greenos_datadir=${datarootdir}
            declare -x -r greenos_datadir=${datarootdir}
	else
	    declare -x -r greenos_datadir=$greenos_datarootdir
            declare -x -r greenos_datadir=$greenos_datarootdir
	fi
    fi
    if test -z "$greenos_htmldir" ; then
	if test -n "${docdir}" ; then
	    declare -x -r greenos_htmldir=${docdir}
	else
	    declare -x -r greenos_htmldir=$greenos_datarootdir/html
	fi
    fi
    if test -z "$greenos_infodir" ; then
	if test -n "${prefix}/share/info" ; then
	    declare -x -r greenos_infodir=${prefix}/share/info
	else
	    declare -x -r greenos_infodir=$greenos_datarootdir/info
	fi
    fi
    if test -z "$greenos_mandir" ; then
	if test -n "${prefix}/share/man" ; then
	    declare -x -r greenos_htmldir=${prefix}/share/man
	else
	    declare -x -r greenos_htmldir=$greenos_datarootdir/man
	fi
    fi
    if test -z "$greenos_localedir" ; then
	if test -n "${datarootdir}/locale" ; then
	    declare -x -r greenos_localedir=${datarootdir}/locale
	else
	    declare -x -r greenos_localedir=$greenos_datarootdir/locale
	fi
    fi
    if test -z "$greenos_localstatedir" ; then
	if test -n "${prefix}/var" ; then
	    declare -x -r greenos_localstatedir=${prefix}/var
	else
	    declare -x -r greenos_localstatedir=$greenos_prefix/var
	fi
    fi
    if test -z "$greenos_sharedstatedir" ; then
	if test -n "${prefix}/com" ; then
	    declare -x -r greenos_sharedstatedir=${prefix}/com
	else
	    declare -x -r greenos_sharedstatedir=$greenos_prefix/com
	fi
    fi
    if test -z "$greenos_sysconfdir" ; then
	if test -n "${prefix}/etc" ; then
	    declare -x -r greenos_sysconfdir=${prefix}/etc
	else
	    declare -x -r greenos_sysconfdir=$greenos_prefix/etc
	fi
    fi
    if test -z "$greenos_op_templates" ; then
	declare -x -r greenos_op_templates=$greenos_datadir/greenos-op/templates
        declare -x -r greenos_op_templates=$greenos_datadir/greenos-op/templates
    fi
    if test -z "$greenos_cfg_templates" ; then
	declare -x -r greenos_cfg_templates=$greenos_datadir/greenos-cfg/templates
        declare -x -r greenos_cfg_templates=$greenos_datadir/greenos-cfg/templates
    fi
    if test -z "$greenos_configdir" ; then
	declare -x -r greenos_configdir=$greenos_prefix/config
        declare -x -r greenos_configdir=$greenos_prefix/config
    fi

    for var in prefix exec_prefix datarootdir ; do
	eval test -n \"\$_greenos_save_$var\" \&\& $var=\$_greenos_save_$var
    done

    # It's not like we do, or should support installing GreenOS at a different prefix
    declare -x -r greenos_libexec_dir=/usr/libexec/greenos
    declare -x -r greenos_bin_dir=/usr/bin
    declare -x -r greenos_sbin_dir=/usr/sbin
    declare -x -r greenos_share_dir=/usr/share
    declare -x -r gosconf_bin_dir=/usr/libexec/greenos/gosconf/bin

    if test -z "$greenos_conf_scripts_dir" ; then
        declare -x -r greenos_conf_scripts_dir=$greenos_libexec_dir/conf_mode
    fi
    if test -z "$greenos_op_scripts_dir" ; then
        declare -x -r greenos_op_scripts_dir=$greenos_libexec_dir/op_mode
    fi
    if test -z "$greenos_completion_dir" ; then
        declare -x -r greenos_completion_dir=$greenos_libexec_dir/completion
    fi
    if test -z "$greenos_validators_dir" ; then
        declare -x -r greenos_validators_dir=$greenos_libexec_dir/validators
    fi
    if test -z "$greenos_data_dir" ; then
        declare -x -r greenos_data_dir=$greenos_share_dir/greenos
    fi
    if test -z "$greenos_persistence_dir" ; then
        UNION_NAME=$(cat /proc/cmdline | sed -e s+^.*greenos-union=++ | sed -e 's/ .*$//')
        declare -x -r greenos_persistence_dir="/usr/lib/live/mount/persistence/${UNION_NAME}"
    fi
    if test -z "$greenos_rootfs_dir" ; then
        ROOTFS=$(mount -t squashfs | grep loop0 | cut -d' ' -f3)
        declare -x -r greenos_rootfs_dir="${ROOTFS}"
    fi
    if test -z "$VRF" ; then
        VRF=$(ip vrf identify)
        [ -n "$VRF" ] && declare -x -r VRF="${VRF}"
    fi
    if test -z "$NETNS" ; then
        NETNS=$(ip netns identify)
        [ -n "$NETNS" ] && declare -x -r NETNS="${NETNS}"
    fi

} 2>/dev/null || :

[ -r /etc/default/greenos-cfg ] && source /etc/default/greenos-cfg

[ -r /etc/default/greenos-local-env ] && source /etc/default/greenos-local-env

###  Local Variables:
###  mode: shell-script
###  End:
