#! /bin/sh -c "echo This script should not be run directly.; exit 1"
################################################################
# Myricom GM networking software and documentation
# Copyright 2001, 2002 by Myricom, Inc.
# All rights reserved.	 See the file `COPYING' for copyright notice.
################################################################
# author: glenn@myri.com
# $Source: /repository/gm/scripts/GM_INSTALL,v $

################################################################
# Command line processing
################################################################

while true
do
    case "$1" in
    	"")
	    # We are done processing the command line
	    break
	    ;;
    	--help | -h | /?)
            gm_echo "$0 <dir>" | sed -e 's@.*/@@'
            gm_echo "  Installs GM in directory <dir>."
            gm_echo "$0 [options]" | sed -e 's@.*/@@'
            gm_echo "options:"
            gm_echo "  --help | -h | /?     Print these instructions"
	    gm_echo "  --copy               Install copies of files, not links"
            gm_echo "  --link	        Install links, not copies [default]"
            gm_echo "  --prefix=<full_path> Install GM at <full_path>"
            gm_echo "  --verbose            Print debugging information"
            gm_echo "  <full_path>          Install GM at <full_path>"
	    exit 0
            ;;
	--link)
	    gm_link_not_copy=1
	    ;;
	--prefix=* | --prefi* | --pref=* | --pre=* | --pr=* | --p=*)
    	    prefix=`echo "$1" | sed -e 's/--p.*=//'`
	    if test x"$prefix" != x"$gm_configured_prefix"
	    then
		gm_echo "Installing in \"$prefix\""
		gm_echo "instead of default \"${gm_configured_prefix}\"."
	    fi
	    ;;
 	/*)
    	    prefix="$1"
	    if test x"$prefix" != x"$gm_configured_prefix"
	    then
		gm_echo "Installing in \"$prefix\""
		gm_echo "instead of default \"${gm_configured_prefix}\"."
	    fi
	    ;;
    	--verbose)
    	    gm_verbose=1
    	    ;;
	*)
	    gm_die "Option \"$1\" is not recognized."
    esac
    shift
done

# Make sure we can access the install directory.

foo=`gm_canonical_dirname "${prefix}/."` \
|| gm_die "Could not access directory ${prefix}."
gm_echo "Installing in ${prefix}."

# change directory to the binary directory.

gm_this_script_dir=`gm_dirname "$gm_this_script"` \
|| gm_die "Could not determine script directory name."
cd "$gm_this_script_dir" \
|| gm_die "Could not enter the \"$gm_this_script\" directory"
gm_this_script_dir=`gm_canonical_dirname "$gm_this_script_dir/."` \
|| gm_die "Could not canonicalize directory name \"$gm_this_script_dir\""
gm_make_id_matches "$gm_this_script_dir" \
|| gm_die "Internal build ID mismatch for \"$gm_this_script_dir\""

# Note where we can find the libtool script.

gm_libtool="./.gm_uninstalled_apps/etc/gm/gm_libtool"

# cwd=`gm_pwd` || gm_die "Could not determine current working directory."
# 
# if test "${prefix}" = "${cwd}"
# then
#     gm_echo "Installing in place."
#     in_place=1
# else
#     in_place=0
# fi

# Now that we know the prefix we want to install in, start logging there.

gm_start_install_log

################################################################
# 
################################################################

# gm_print_install_destination_or_die <prefix> <sourcefile>
#
# Output the installation destination for a given prefix and
# sourcefile, where the sourcefile is a relative path.  This just
# strips any "./" prefix from sourcefile, strips any ".gm_uninstalled..."
# component from the path, and prepends prefix to the result.

gm_print_install_destination_or_die ()
{
    gm_install_destination=`echo "$2" | sed -e 's@/.gm_uninstalled[^/]*@@'` \
    || gm_die "gm_print_install_destination_or_die: internal error 1"
    # gm_install_destination=`echo "$gm_install_destination" | sed -e 's@^./@@'`\
    # || gm_die "gm_print_install_destination_or_die: internal error 2"
    printf "%s" "$1/$gm_install_destination" \
    || gm_die "gm_print_install_destination_or_die: internal error 3"
}

gm_install_shared ()
{
    # Determine the version of any installed GM version.
    test -f ${prefix}/etc/gm/.gm_installed_make_id \
    && . ${prefix}/etc/gm/.gm_installed_make_id

    # Return without installing the shared parts if GM is already installed.
    # This allows the shared parts to be installed as non-root while the
    # final install must be done as root.
    if test "x$GM_MAKE_ID" = "x$GM_INSTALLED_MAKE_ID"
    then
	gm_echo "Not installing shared parts, which are already installed."
	return 0
    fi

    # Mark GM as not installed by removing the record of the install

    test -f ${prefix}/etc/gm/.gm_installed_make_id \
    && rm -f ${prefix}/etc/gm/.gm_installed_make_id
    
    # # unload any loaded GM drivers
    # 
    # gm_echo "Unloading installed GM drivers"
    # gm_arch_unload_drivers || gm_die "Could not unload drivers."

    # Make sure that the install directory exists

    test -d "${prefix}" \
	|| gm_die "\"${prefix}\" must exist and be a directory."

    # Make sure we can entry the directory.
	
    test -x "${prefix}" || gm_die "${prefix} is not executable."
	
    # Get the real name of the prefix directory

    (cd "${prefix}") || gm_die "Cannot change directory to \"${prefix}\""
    tmp=`cd ${prefix} && gm_pwd` \
    || gm_die "Cannot determine real path to \"${prefix}\""
    prefix="${tmp}"

    # Confirm that the installation directory is readable and writable.

    test -r "${prefix}" || gm_die "${prefix} is not readable."
    test -w "${prefix}" || gm_die "${prefix} is not writable."
    
    # 
    # ################
    # # Check destination directory for installability
    # ################
    # 
    # # pwd=`gm_pwd` || gm_die "Could not determine path."
    # # prefix=`cd ${prefix} && gm_pwd` \
    # # || gm_die "Could not canonicalize ${prefix}."
    # # test ${pwd} = ${prefix} \
    # # || gm_die "Cannot install in the binary release directory ${prefix}"
    # # test -w ${prefix} || gm_die ${prefix} is not writable.
    # 
    # # Confirm that we can create subdirectories in the installation directory
    # mkdir -p ${prefix}/.mkdir_test_directory \
    # || gm_die "Could not make subdirectory in ${prefix}"
    # 
    # # Confirm that we can create files in these subdirectories
    # touch ${prefix}/.mkdir_test_directory/.touch_test_file \
    # || gm_die "Cannot create files in subdirectories of ${prefix}"
    # 
    # # Confirm that we ca set and clear permissions on these files
    # chmod a+rwx ${prefix}/.mkdir_test_directory/.touch_test_file \
    # || gm_die "Cannot set permissions on files in ${prefix}"
    # chmod a-rwx ${prefix}/.mkdir_test_directory/.touch_test_file \
    # || gm_die "Cannot clear permissions on files in ${prefix}"
    # 
    # # clean up the test files
    # rm -rf ${prefix}/.mkdir_test_directory

    ################
    # Create installation log
    ################

    gm_create_uninstall_file "${prefix}/sbin/GM_UNINSTALL" \
    || gm_die "Could not create uninstall file \"${prefix}/sbin/GM_UNINSTALL\""
    
    ################
    # Create installation directories
    ################

    gm_echo "Creating installation directories."
    for dir in `find . -type d`
    do
	dir=`echo ${dir} | sed -e 's@/.gm_uninstalled[^/]*@@' | sed -e 's@^./@@'`
	last_part=`gm_basename ${dir}`
	# Skip libtools '.libs' directories
	if test "$last_part" = ".libs" ; then continue ; fi
	# gm_echo "Creating ${prefix}/${dir}."
	gm_mkdir "${prefix}/${dir}" \
	|| gm_die "Could not make installation directory ${prefix}/${dir}"
	gm_progress "${prefix}/${dir}"
    done

    ################
    # Install libraries
    ################

    # Libraries must be installed before any programs that might depend
    # on the libraries.

    gm_echo "Installing libraries."
    for file in \
    `find ./.gm_uninstalled_libs -name .libs -prune -o -name '*.la' -print`
    do
	# gm_echo ${file}
	destination=`gm_print_install_destination_or_die "$prefix" "$file"`
	installed_libs="${destination} ${installed_libs}"
	gm_install_lib "$file" "$destination" \
	|| gm_die "Could not install \"${file}\" as \"${destination}\"."
	gm_progress "$destination"
    done

    ################
    # Install applications
    ################

    # Apps must be installed only after libraries, a libtool constraint.

    gm_echo "Installing applications."
    for file in \
    `find ./.gm_uninstalled_apps -name .libs -prune -o -type f -print`
    do
	# Skip the .mkdir files
        if test x`echo "$file"` != x`echo "$file" | sed -e 's/.mkdir//'`
        then
	    continue
        fi
    
        destination=`gm_print_install_destination_or_die "$prefix" "$file"`
    
        gm_install_program "$file" "$destination" \
        || gm_die "Could not install ${file} as ${destination}."
        
        gm_progress "$destination"
    done

    ################
    # Install all other files
    ################

    gm_echo "Installing other files."
    for file in `find ./.gm_uninstalled_other -type f`
    do
	# Skip the .mkdir files
        if test x`echo "$file"` != x`echo "$file" | sed -e 's/.mkdir//'`
        then
	    continue
        fi
    
	destination=`gm_print_install_destination_or_die "$prefix" "$file"`

	# Copy the file
        gm_cp "$file" "$destination" \
        || gm_die "Could not create file \"${prefix}/${file}\""
	gm_progress "${prefix}/${file}"
    done

    # gm_arch_install_drivers || gm_die Could not install drivers
    # 
    # ################
    # # Run lots of installation tests
    # ################
    # 
    # gm_echo Running installation tests.
    # 
    # mkdir -p ${prefix}/log \
    # || gm_die "Could not make directory ${prefix}/log."
    # 
    # touch ${prefix}/etc/gm/install_log \
    # || gm_die Could not create ${prefix}/etc/gm/install_log
    # 
    # ${prefix}/etc/gm/gm_config_info 0 > ${prefix}/etc/gm/install_log \
    # || (gm_echo "Error log is ${prefix}/etc/gm/install_log"\
    #     ; gm_die "Installation testing failed.")

    ################
    # Finish library installation, as required by libtool.  This is done last
    # so any instructions about using shared libraries will hopefully
    # be visible on the user's display when the script exits.
    ################

    installed_gm_libtool=`gm_print_install_destination_or_die "$prefix" "$gm_libtool"`
    for lib in ${installed_libs}
    do
	libdir=`echo ${lib} | sed -e 's@/[^/]*$@@'`
	gm_libtool_finish ${libdir} \
	|| gm_die "Could not finish library installation for ${libdir}."
	gm_log_uninstall_command "$installed_gm_libtool --mode=uninstall rm -f \"${lib}\"" 
    done

    ################
    # Mark GM as installed
    ################

    gm_log_uninstall_command "rm -f \"${prefix}/etc/gm/.gm_installed_make_id\"" \
    && (sed -e 's/GM_MAKE_ID/GM_INSTALLED_MAKE_ID/' \
	< ./.gm_make_id \
	> ${prefix}/etc/gm/.gm_installed_make_id)
    status=$?
    if test $? != 0
    then
	rm -f ${prefix}/etc/gm/.gm_installed_make_id
	return $status
    fi

    gm_close_uninstall_log || gm_die "Could not close uninstall script"
}

################
# Perform the operations requested on the command line
################

gm_install_shared || gm_die Could not install GM shared components.
gm_echo "GM shared parts are now installed.  Remember to run"
gm_echo "	${prefix}/sbin/gm_install_drivers"
gm_echo "as root on each machine that will use GM to make GM usable"
gm_echo "on that machine."

exit 0
