#!/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"
docdir="/usr/share/doc"
version="1.7.39"
ret=""
overall=""
                
failure() {
    echo "Operation failed$1"
    exit 1
}

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

    if [ -z "$ret" ]; then
        ret="$2"
    else
	flag=0
	while [ $flag -ne 1 ]; do 
            case $ret in
            y* | Y*)
                ret="y"
                flag=1
                ;;
            a* | A*)
                if [ "$3" != "y/n" ]; then
                    ret="a" 
                    flag=1
                else  
                    echo -n "$1 [$2] "
                    read ret
                    if [ -z "$ret" ]; then
                        ret="$2"
                        flag=1
                    fi  
                fi
                ;;
            n* | N*)
                ret="n"
                flag=1
                ;;
            *)
                echo -n "$1 [$2] "
                read ret
                if [ -z "$ret" ]; then
                    ret="$2"
                    flag=1
                fi  
                ;;
            esac
        done 
    fi
}

careful_copy() {
    copy="y"
    base=`basename "$1"`
    if [ -f "$2/$base" -a -z "$overall" ]; then
        prompt_yesno "File \"$2/$base\" exists, overwrite (y/n/a) ?" "y" "y/n/a"
        if [ "$ret" = "a" ]; then
            overall="y"
        elif [ "$ret" != "y" ]; then
            copy="n"
        fi
    fi

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

    return 0
}

careful_remove() {
    remove="y"
    base=`basename "$1"`
    
    if [ -f "$2/$base" ]; then
        
	if [ -z "$overall" ]; then
            prompt_yesno "Remove file \"$2/$base\" (y/n/a) ?" "y" "y/n/a"
            if [ "$ret" = "a" ]; then
                overall="y"
            elif [ "$ret" != "y" ]; then
                remove="n"
            fi
        fi

        if [ "$remove" != "n" ]; then 
            /bin/rm -f "$2/$base" || return 1
        fi
	
    elif [ -d "$2/$1" ]; then
    
	if [ -z "$overall" ]; then
            prompt_yesno "Remove directory \"$2/$1\" and all its contents (y/n/a) ?" "y" "y/n/a"
            if [ "$ret" = "a" ]; then
                overall="y"
            elif [ "$ret" != "y" ]; then
                remove="n"
            fi
        fi

        if [ "$remove" != "n" ]; then 
            /bin/rm -rf "$2/$1" || return 1
        fi 
	
    else 
        echo "Error: wrong file type of \"$2/$1\" "    
    fi 

    return 0
}

echo "Installing procfgd in $installdir... "
/bin/ps -C procfgd > /dev/null
if [ $? -eq 0 ]; then  
    prompt_yesno "Procfgd is running. Are you sure you want to continue (y/n) ?" "y" "y/n"
    if [ "$ret" == "n" ]; then
        echo "Exiting..."
        exit 0
    fi	   
fi

careful_copy "procfgd" "$installdir" || failure
careful_copy "procfgd_adduser" "$installdir" || failure

/bin/mkdir -p /etc/procfgd/

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
verdirs=$(/bin/ls $docdir | /bin/grep procfgd | /bin/grep -v "grep") 

for verdir in $verdirs ; do
    if [ "$verdir" != "procfgd-$version" ]; then
        careful_remove "$verdir" "$docdir" || failure
    fi 
done

/bin/mkdir -p $docdir/procfgd-$version

echo "Installing documentation in $docdir... " 
for file in README LICENSE* ldistrib.txt ; do
    careful_copy "$file" "$docdir/procfgd-$version" || 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" "y/n"
if [ "$ret" = "y" ]; then
    ./INSTALL_BOOT install "$overall"
fi

