#!/bin/sh
#
# Install one or more groups from a source release non-interactively.
# `bin/GROUP.group-all' and `bin/GROUP.group-all-modules' must be
# built. 
#
# Options are passed through the environment.
#
#    $groups:  Groups to install.  (See the `grp' directory.) 
#
#    $install_dir:  Base installation dir (`/usr' or the home
#                   directory).  Appended to $install_root. 
#
#    $install_root:  Prepended to $install_dir.  When installing
#                    programs, $install_root is empty.  When not
#                    empty, the directory structure is built there.
#                    Not terminated by '/'. 
#
#    $skip_home:  Don't write files in home directories (e.g. links in
#                 menues) 
#

. sh/ac.sh
. sh/temp.sh
. sh/e-mail.sh
. sh/version-number.sh
. sh/verbose.sh
. sh/tools.sh
. sh/menues.sh
. sh/temp-dir.sh

if [ -z "$install_dir" ]
then
    echo 1>&2 "$0:  *** error: \$install_dir must not be empty."
    exit 1 
fi

echo
echo 1>&4 "Installing in \`$install_dir'."

programs=
for group in $groups
do
    programs="$programs `cat bin/$group.group-all-programs`"
done

#
# Menu entries
#
gnome_menu_dir="$install_root$gnome_main_menu_dir"
kde_menu_dir="$install_root$kde_main_menu_dir"

if [ -z "$install_root"         \
     -a \! -w "$gnome_main_menu_dir"   \
     -a -z "$skip_home" ]
then
    gnome_menu_dir="$gnome_home_menu_dir"
fi

if [ -z "$install_root"         \
     -a \! -w "$kde_main_menu_dir"   \
     -a -z "$skip_home" ]
then
    kde_menu_dir="$kde_home_menu_dir"
fi

#
# Desktop icons
#
if [ -z "$skip_home" ]
then
    gnome_desktop_dir="$HOME/.gnome-desktop"
    kde_desktop_dir="$HOME/Desktop"
else
    gnome_desktop_dir=
    kde_desktop_dir=
fi

#
# Per-program install
#

installed=

for program in $programs
do
    if echo $installed | grep -wv $program >/dev/null 
    then
	sh/install-file bin/baby_$program "$install_root$install_dir/bin/baby_$program"

	interactive=
	if echo $program | grep '^gui_'  >/dev/null ; then interactive=t ; fi

	if [ "$interactive" ] 
	then

	    # Displayed name of an interactive program. 
	    prog_name="`grep '/\* *Name: *[^\*]*\*/' src/$program.cc | $sed 's/\/\* *Name: *\([[:alpha:]][^\*]*[[:alnum:]]\) *\*\//\1/'`"

	    echo 1>&4 "Program [$program] has name [$prog_name]."

	    if [ -z "$prog_name" ] 
	    then
		echo  "*** error:  coudn't find name of $program." 
	    fi

	    tmp_entry=$temp_dir/${$}_entry

	    {
		echo "[Desktop Entry]"
		echo "Name=$prog_name"
		echo "Comment=`grep '/\* *Comment: *[^\*]*\*/' < src/$program.cc | $sed 's/\/\* *Comment: *\([^\*]*\) *\*\//\1/'`"
		echo "Exec=$install_dir/bin/baby_$program"
		echo "Terminal=0"
		echo "Type=Application"
		icon_name=`grep '/\* *Icon: *[^\*]*\*/' < src/$program.cc | \
                          $sed 's/\/\* *Icon: *\([^\* ]*\) *\*\//\1/'`
		if [ -z "$icon" ] ; then icon=baby ; fi
                icon=$install_dir/share/baby/$version_number/desktop/$icon_name.png
		echo "Icon=$icon"
		mimetype=`grep '/\* *MimeType: *[^\*]*\*/' < src/$program.cc | \
                          $sed 's/\/\* *MimeType: *\([^\* ]*\) *\*\//\1/'`
		mimetype=$mimetype
		if [ "$mimetype" ]
		then
		    echo "MimeType=$mimetype;Application"
		fi
	    } >$tmp_entry

	    sh/install-file $tmp_entry "$gnome_menu_dir/$prog_name.desktop"
	    sh/install-file $tmp_entry "$kde_menu_dir/$prog_name.desktop"

	    if [ "$gnome_desktop_dir" ]
	    then
		sh/install-file $tmp_entry "$gnome_desktop_dir/$prog_name"
	    fi
	    if [ "$kde_desktop_dir" ]
	    then
		sh/install-file $tmp_entry "$kde_desktop_dir/$prog_name"
	    fi

	    # Desktop icon image
	    icon_file=desktop/$icon_name.png
	    sh/install-file $icon_file $install_root$install_dir/share/baby/$version_number/$icon_file

	fi # interactive

	installed="$installed $program"
    
    fi # ! installed 
done

modules=
for group in $groups
do
    modules="$modules `cat bin/$group.group-all-modules`"
done

echo 1>&4 "Modules are:     $modules"

sh/install-file COPYING $install_root$install_dir/share/baby/$version_number/COPYING

install -d $install_root$install_dir/share/baby/$version_number/share

for module in $modules
do
    if [ -d share/$module ]
    then
	sh/do rm -rf 
	sh/do cp -r share/$module $install_root$install_dir/share/baby/$version_number/share
 	sh/chmod-755 $install_root$install_dir/share/baby/$version_number/share/$module
    fi
done

#
# Mime entries
#
mimelnk_dir=$install_root$main_mimelnk_dir
if [ -z "$install_prefix" -a \! -w $mimelnk_dir ]
then
    mimelnk_dir=$home_mimelnk_dir
fi

for file in mimelnk/[!+]*desktop
do
    name=$temp_dir/${$}.mime
    $sed s%ICON_DIR%$install_dir/share/baby/$version_number/desktop% <$file >$name
    end="`basename $file`"
    sh/install-file $name $mimelnk_dir/application/$end
done

echo 1>&4 "Installation successful."

exit 0
