#!/bin/sh
#############################################################
# Copyright (C) 2005 Parallels Inc. All Rights Reserved.
# Configure application
#############################################################

. /usr/lib/parallels/gettext.sh

export TEXTDOMAIN="parallels"

curdir="`pwd`"

v_os="LINUX"
v_path=/usr/lib/parallels

# Get current version 
v_maj="`grep 'VER_MAJOR' $v_path/parallels-version | awk '{print $2;}'`"
v_min="`grep 'VER_MINOR' $v_path/parallels-version | awk '{print $2;}'`"
v_build="`grep 'VER_BUILD' $v_path/parallels-version | awk '{print $2;}'`"

mainname="Parallels"
submname="Workstation"
mainver="$v_maj.$v_min"

if test `id -u` -ne 0; then
    gettext "Error: you must be root to run this script"; echo
    exit
fi

echo
eval_gettext "Configuring \$mainname \$submname \$mainver build \$v_build"; echo
echo

# Display EULA
if test ! -e $v_path/.ereaded; then
    echo
    echo
    echo
    eval_gettext "You must read and accept license agreement to run \$mainname \$submname \$mainver"; echo
    gettext "To list by pages use Spacebar key."; echo
    gettext "Press 'ENTER' to continue..."; echo
    echo
    echo
    read
    
    more $v_path/doc/LICENSE

    echo
    echo
    response="..."
    while (test "xxx`echo $response | egrep -i 'y$|yes$|n$|no$'`" = xxx); do
        gettext "Do you accept terms of license agreement ? (Y/N)"; echo
        read response
    done

    if test "xxx`echo $response | egrep -i 'n$|no$'`" != "xxx"; then
        cd $curdir
        exit
    else
        touch $v_path/.ereaded > /dev/null 2>&1
    fi
fi

echo
echo

cd $v_path

$v_path/autostart/drivers_stop > /dev/null 2>&1

tmpf=$v_path/comp.log.$$
eval_gettext "     Configuring \$mainname \$submname \$mainver drivers..."; echo

if [ "$v_os" == "LINUX" ]; then
    chkfile=include/linux/mm.h
    kernel=/lib/modules/`uname -r`
    if [ -f $kernel/sources/$chkfile ]; then
        OS_SRC=$kernel/sources
    elif [ -f $kernel/build/$chkfile ]; then
        OS_SRC=$kernel/build
    elif [ -f /usr/src/linux/$chkfile ]; then
        OS_SRC=/usr/src/linux
    else
    
        echo
        gettext "     Unable to find linux kernel source."; echo
        gettext "     To configure Parallels Workstation 2.1 you should have"; echo
        gettext "     kernel source package for your Linux system installed."; echo
        echo
        gettext "     Do you want to get more information about the kernel"; echo
        gettext "     source package installation procedure? (Y/N) "; echo -n
 
        answer=''
        while (test -z "`echo $answer | grep -Ei '^(y|yes|n|no)$'`"); do
            read answer
        done 

        if [ -n "`echo $answer | grep -Ei '^y.*$'`" ]; then
            helpfile="/tmp/parallels-minihelp"
            gettext "
To install kernel source:

If you use RPM-based system:
     1. Insert distribution CD into CD-ROM drive of your computer.
        Number of the CD for your system see in the table below.
     2. Mount CD-ROM.
     3. Change the current directory to the one which CD-ROM is
        mounted to (usually it is /mnt/cdrom or /media/cdrom).
     4. In the terminal issue the command:
        find . -name <package name> | xargs rpm -i
        Name of the package for your system see in the table below.

     System          CD#     Package Name
     ---------------------------------------------------------------
     Red Hat 9.0     2       kernel-source-2.4.20-8.i386.rpm
     RHEL 4          3       kernel-devel-2.6.9-5.EL.i686.rpm
     SUSE 9.0        3       kernel-source-2.4.21-69.i586.rpm
     SUSE 9.1        3       kernel-source-2.6.4-52.i586.rpm
     SUSE 9.2        5       kernel-source-2.6.8-24.i586.rpm
     SUSE 9.3        5       kernel-source-2.6.11.4-20a.i586.rpm
     Fedora Core 4   4       kernel-devel-2.6.11-1.1369_FC4.i686.rpm

 
If you use Debian-based system:
     In the terminal issue the following command
       apt-get install kernel-headers-2.4.27-2-386
     Please note that you should choose kernel headers package
     corresponding to your kernel. You can determine version of
     your kernel using command 'uname -r'.
     You can check the list of available kernel headers packages using
     the command 'apt-cache search kernel-headers'.
     
     If the package has not been found, try to run the following command:
       apt-setup
     and specify the path to another package archive source." > $helpfile
            less $helpfile; rm -f $helpfile
        fi

        exit -2
    fi
fi

# configure
./configure >> $tmpf 2>&1

if test $? -eq 0; then
    eval_gettext "     Compiling \$mainname \$submname \$mainver drivers..."; echo
    make clean all >> $tmpf 2>&1
    err=$?

    if test $err -eq 0; then
        gettext "     Drivers have been compiled successfully."; echo
    else
        mv -f $tmpf $tmpf.error
        echo
        eval_gettext "Can not compile and/or link drivers. Read \"\$v_path/doc/INSTALL\"
and follow instructions specified in this document.

Configuration and compilation log is available at \$tmpf.error"; echo
        exit
    fi
else
    mv -f $tmpf $tmpf.error
    echo
    eval_gettext "Can not configure! Read \"\$v_path/doc/INSTALL\"
and follow instructions specified in this document.

Error log is available at \$tmpf.error
Also you can check configuration log at \$v_path/config.log"; echo
    exit
fi

rm -f $tmpf

pushd `pwd` > /dev/null && cd $v_path
lic_name='.parallels_license_2.1'
if [ ! -e "${lic_name}" ]; then
	if [ -e "${lic_name}R" ]; then
		cp -f ${lic_name}R $lic_name
	elif [ -e "${lic_name}B" ]; then
		cp -f ${lic_name}B $lic_name
	else
		touch $lic_name
	fi
fi
chmod 0666 $lic_name
popd > /dev/null

touch $v_path/.parallels_common_options
      
chmod 666 $v_path/.parallels_common_options

if test ! -s $v_path/.dhcpd_configuration; then 

cat > $v_path/.dhcpd_configuration << EOF
[vnic0]
DHCP=1
Scope start=10.37.129.1
Scope end=10.37.129.254
Scope network mask=255.255.255.0
Start interface=1
EOF

chmod 666 $v_path/.dhcpd_configuration

fi

if [ -d /etc/sysconfig/network-scripts/ ]; then
	echo BOOTPROTO=none > /etc/sysconfig/network-scripts/ifcfg-vnic0
fi

gettext "     Installing drivers..."; echo
$v_path/autostart/iscripts install > /dev/null 2>&1

gettext "     Starting drivers..."; echo
$v_path/autostart/drivers_start || {
    eval_gettext "\$mainname \$submname drivers were successfully configured
and compiled but cannot be started (insmod command failed).
Run 'dmesg' command for more information."; echo
    exit -1
}

$v_path/tools/mimelink associate

rm -f $v_path/.not_configured

eval_gettext "Parallels Workstation has been successfully configured

Now you can run \$mainname \$submname \$mainver
    Issue \"parallels\" command."; echo; echo 

cd $curdir
