#!/bin/sh
#   /***************************************************************************
#   **                                                                        **
#   **  License                                                               **
#   **  =======                                                               **
#   **                                                                        **
#   **  This software program is released under the terms of a license        **
#   **  agreement between you ('Licensee') and Intel. Do not use or load this **
#   **  software or any associated materials (collectively, the 'Software')   **
#   **  until you have carefully read the full terms and conditions of the    **
#   **  LICENSE located in this software package. By loading or using the     **
#   **  Software, you agree to the terms of this Agreement. If you do not     **
#   **  agree with the terms of this Agreement, do not install or use the     **
#   **  Software.                                                             **
#   ***************************************************************************/
#
#   /***************************************************************************
#   **                                                                                                                                                                 **
#   ** INTEL CORPORATION                                                                                                                **
#   **                                                                                                                                                                 **
#   ** This software is supplied under the terms of the license included                                          **
#   ** above.  All use of this software must be in accordance with the terms                                  **
#   ** of that license.                                                                                                                                      **
#   **                                                                                                                                                                  **
#   **  Abstract:                                                                                                                                              **
#   **    Installation script for PROCfgd                                                                                                   **
#   **                                                                                                                                                                 **
#   ***************************************************************************/

installdir="/usr/sbin"
ret=""
overall=""

prompt_yesno() {
    echo -n "$1 [$2] "
    read ret

    if [ -z "$ret" ]; then
        ret="$2"
    else
        # look only at the first char of $ret
        ret=${ret:0:1}
    fi
}

careful_copy() {
    copy="y"
    base=`basename "$1"`
    if [ -f "$2/$base" -a -z "$overall" ]; then
        prompt_yesno "$2/$base exists. Overwrite? (y|n|a)" "y"
        if [ "$ret" = "a" -o "$ret" = "A" ]; then
            overall="y"
        elif [ "$ret" != "y" -a "$ret" != "Y" ]; then
            copy="n"
        fi
    fi

    if [ "$copy" != "n" ]; then
        /bin/cp -f "$1" "$2" || return 1
    fi

    return 0
}

failure() {
    echo "operation failed"
    exit 1
}

echo "installing procfgd in $installdir... "
careful_copy "procfgd" "$installdir" || failure
careful_copy "procfgd_adduser" "$installdir" || failure

man_cfg="/etc/man.config"
man_type="man1"
dirlist=`[ -e $man_cfg ] && grep -e "^MANPATH[^_]" $man_cfg | awk '{print $2}'`

if [ -z "$dirlist" ]; then
    dirlist="/usr/share/man /usr/man"
fi

# prune the list down to only values that exist
for dir in $dirlist ; do
    if [ -e "$dir/$man_type" ]; then
         man_path="$dir"
         break
    fi
done

if [ -z "$man_path" ]; then
    man_path="/usr/man"
fi

man_path="$man_path/$man_type"

echo "installing procfgd man page (procfgd.1) in $man_path... "
/bin/rm -f $man_path/procfgd.1
careful_copy "procfgd.1.gz" "$man_path" || failure

# documentation installation
docdir="/usr/share/doc/procfgd-"1.4.28""
mkdir -p $docdir

echo "installing documentation in $docdir... " 
for file in README LICENSE* ldistrib.txt ; do
    careful_copy "$file" "$docdir" || failure
done
 
# xerces library installation
xerces_tar="libxerces-c1_6_0.tar.gz"
xerces_lib="libxerces-c1_6_0.so"
libdir="/usr/lib"
no_tar="FALSE"
copy="FALSE"

if [ ! -f $libdir/$xerces_lib ]; then
    if [ -f $PWD/../$xerces_tar ]; then
        xerces_path="$PWD/../$xerces_tar"
        copy="TRUE"
    elif [ -f $PWD/$xerces_tar ]; then
        xerces_path="$PWD/$xerces_tar"
    elif [ -f $PWD/../$xerces_lib ]; then
        xerces_path="$PWD/../$xerces_lib"
        copy="TRUE"
        no_tar="TRUE"
    elif [ -f $PWD/$xerces_lib ]; then
        xerces_path="$PWD/$xerces_lib"
        no_tar="TRUE"
    else
        xerces_path="NONE"
    fi

    if [ $xerces_path != "NONE" ]; then
        echo "installing $xerces_lib in $libdir... "
        if [ $copy == "TRUE" ]; then
            careful_copy "$xerces_path" "."
        fi
        if [ $no_tar == "FALSE" ]; then
            tar xzf $xerces_tar || failure
        fi
        careful_copy "$xerces_lib" "$libdir" || failure
    else
        echo "Missing $xerces_tar file. Please consult README file."
    fi
fi

prompt_yesno "Do you want procfgd to start on boot? (y|n)" "y"
if [ "$ret" = "y" -o "$ret" = "Y" ]; then
    ./INSTALL_BOOT install "$overall"
fi

