# -*-Shell-script-*-
# 
# functions     This file contains common functions and logging facility 
#               for all scripts in postinst directory used by the binary
#               installer and the rpm %post section.



umask 022

if [ "$POSTINST_DIR" = "" ] ; then
    echo ASSERT: Expect postinst scripts to set POSTINST_DIR environment variable!
    return 1
fi


if [ "${OSTYPE:0:7}" = "solaris" ] ; then
    # Solaris requires this setup to use POSIX utilities.
    PATH=/usr/xpg4/bin:${PATH}
fi

export INSTDIR=$(cd $POSTINST_DIR; cd ..; pwd)

# Needed in install_gconf_settings.sh.
export LOGFILE=$INSTDIR/install.log

if [ -f $POSTINST_DIR/profile.d/realplay.sh ]; then
    PACKAGE=realplay
elif [ -f $POSTINST_DIR/profile.d/hxplay.sh ] ; then
    PACKAGE=hxplay
else
    echo 
    echo Failed to determine PACKAGE name!
    echo 
    exit 1
fi
export PACKAGE

export TARGET=$PACKAGE


# TODO: This needs to be compile-time to generate .mo file.
# Do NOT use echo $"whatever!". "!" cause errors!
export TEXTDOMAIN=$PACKAGE

ID=$(id -u)

# The Portland Project recommended setting PATH this way.
# Refer to: http://portland.freedesktop.org/wiki/
PATH=${PATH}:$POSTINST_DIR/xdg-utils


# Convenient environment variable for commands/scripts that should be 
# run as user, instead of root, such as xdg-desktop-icon.
# su requires double-quotation marks around the command and thus
# conflicts with "eval $*". Use logcmd instead!
AS_USER=
if [ "$ID" = "0" ] ; then
    if [ "$LOGNAME" != "" ] ; then
        AS_USER="su $LOGNAME -c -- "
    elif [ "$USER" != "" ] ; then
        AS_USER="su $USER -c -- "
    fi
else
    # This is needed to execute command in double-quotation marks, which 
    # could result in the following error:
    # bash: xdg-desktop-icon install --novendor realplay.desktop: No such file or directory
    AS_USER="eval "
fi

# Example: 
# Example: $AS_USER "gconftool-2 --type=string --set /desktop/gnome/url-handlers/rtsp/command \"${TARGET} %s\""
#   This above example, with options, isn't suitable for eval_cmd!



if [ "$ID" = "0" ]; then
    LOCALEPATH=/usr/share/locale
else
    # TODO: Find out if there is a local locale directory.
    LOCALEPATH=~/.local/share/locale
fi



# For command and logging
eval_cmd()
{
    eval $*

    # Log only when write access is granted.
    if [ -n "$LOGFILE" -a \( -w $LOGFILE -o ! -e $LOGFILE \) ] ; then
	echo $* >> $LOGFILE
    fi
}

# For logging command only.
logcmd()
{
    # Log only when write access is granted.
    if [ -n "$LOGFILE" -a \( -w $LOGFILE -o ! -e $LOGFILE \) ] ; then
	echo $* >> $LOGFILE
    fi
}


assert_root_privilege()
{
    if [ "$ID" != "0" ] ; then
        echo 
        echo $"root privilege needed."
        echo 
        exit 1
    fi
}

last_cmd_status_check()
{
    if [ $? -eq 0 ] ; then
        echo $"Succeeded."
    else
        echo
        echo $"Last operation failed."
        echo
        exit 1
    fi
}

dir_writable()
{
    DIR=$1
    if [ "$DIR" == "" ] ; then
        echo "Usage: dir_writable dir_path."
        return 1
    else
        test -d $DIR -a -w $DIR;
        return $?
    fi
}


