#! /bin/sh

# This script asks for variables, which are necessary for building the
# examples of the Office Development Kit. The script duplicates the template
# script and inserts the variables into the copied script.
# The Script was developed for the operating systems Solaris and Linux.

echo "*** PATCH required for OpenOffice1.0.2 only ***"
echo "If you want to build the C++ examples please copy the 'oo1.0.2_sdk_linkpatch' in"
echo "your <office_program_dir> and run it. The script will create some necessary links."
echo "***"

echon () {
    if [ X"$ECHON" = X ]
    then
        # Determine how to "echo" without newline: "echo -n" or "echo ...\c"
        if [ X`echo -n` = X-n ]
            then ECHON=echo; NNL="\c"
            else ECHON="echo -n"; NNL=""
        fi
    fi
    $ECHON "$*$NNL"
}

searchprog () {
    _search=$1; shift

    for _dir in `echo "$PATH" | sed "s/^:/.:/;s/:\$/:./;s/:/ /g"`
    do
        [ -x "$_dir/$_search" ] || continue
        echo "$_dir/$_search"
        return 0
    done
    return 1
}

testVersion () {
    _versionToTest=`echo $1 | tr '[_]' '[.]'`
    _version=`echo $2 | tr '[_]' '[.]'`
    _command=$3; shift

    echo $_versionToTest $_version $_command |
        awk '
            {
              countVersionToTest=split($1,versionToTest,".")
              countVersion=split($2,version,".")
              if ( countVersionToTest < countVersion ) {
                countVersion=countVersionToTest
              }
              for (i=1; i<=countVersion; i++) {
                if ( versionToTest[ i ] < version[ i ] ) {
                  printf "Attention: The version of the command %s is %s . You need at least version %s .\n", $3, $1, $2
                  exit 1
                }
                if ( versionToTest[ i ] > version[ i ] ) {
                  exit 0
                }
              }
              exit 0
            }
        '
}


# Enter installation directory of the Office Development Kit.
OO_SDK_HOME_SUGGESTION=`pwd`
echon "Enter Office Development Kit (6.0 or higher) directory: [$OO_SDK_HOME_SUGGESTION] "
read OO_SDK_HOME

if [ -z "$OO_SDK_HOME" ]
then
    if [ -d $OO_SDK_HOME_SUGGESTION ]
    then
        OO_SDK_HOME=$OO_SDK_HOME_SUGGESTION
    else
        echo "Error: Could not find directory $OO_SDK_HOME_SUGGESTION ."
        echo "Abort."
        exit 0
    fi
fi

if [ -x "$OO_SDK_HOME/idl" -a -d "$OO_SDK_HOME/idl" ]
then
    echon
else
    echo "Could not find directory $OO_SDK_HOME/idl ."
    echo "Abort."
    exit 0
fi


# Enter installation directory of the Office program.
OFFICE_HOME_SUGGESTION=`egrep "=file://" $HOME/.sversionrc | tr -d \\\\r | head -1 | sed -e 's#.*=file://##'`
# check for a client installation (need server directory)
if [ -d $OFFICE_HOME_SUGGESTION ]
then
    # a full installation has a 'program' sub-directory
    if [ ! -d $OFFICE_HOME_SUGGESTION/program -a -h $OFFICE_HOME_SUGGESTION/soffice ]
    then
        # 'soffice' is a symlink, follow it to the server directory
        soserver=`ls -l $OFFICE_HOME_SUGGESTION/soffice | sed -n 's/.* -> //p'`
        soserver=`dirname $soserver`
        OFFICE_HOME_SUGGESTION=`dirname $soserver`
    fi
fi
echo
echo "Enter Office (StarOffice 6.0, StarSuite 6.0, OpenOffice.org 1.0 or higher)"
echon "installation directory: [$OFFICE_HOME_SUGGESTION] "
read OFFICE_HOME

if [ -z "$OFFICE_HOME" ]
then
    if [ -d $OFFICE_HOME_SUGGESTION ]
    then
        OFFICE_HOME=$OFFICE_HOME_SUGGESTION
    else
        echo "Error: Could not find directory $OFFICE_HOME ."
        echo "Abort."
        exit 0
    fi
fi

if [ -x "$OFFICE_HOME/program" -a -d "$OFFICE_HOME/program" ]
then
    echon
else
    echo "Could not find directory $OFFICE_HOME/program ."
    echo "Abort."
    exit 0
fi


# Enter installation directory of the make program.

# Search for the program make.
progpath=`searchprog "make"`

# Determine directory name, and convert it to an absolute path (if necessary)
progdir=`dirname "$progpath"`
case "$progdir" in
    /*)	;;		# absolute path; leave unchanged
    *)	progdir=`cd "$progdir"; pwd`;;
esac

OO_SDK_MAKE_HOME_SUGGESTION=$progdir

echo
echon "Enter GNU make (3.79.1 or higher) tools directory: [$OO_SDK_MAKE_HOME_SUGGESTION] "
read OO_SDK_MAKE_HOME

if [ -z "$OO_SDK_MAKE_HOME" ]
then
    if [ -d "$OO_SDK_MAKE_HOME_SUGGESTION" ]
    then
        OO_SDK_MAKE_HOME=$OO_SDK_MAKE_HOME_SUGGESTION
    else
        echo "Error: Could not find directory $OO_SDK_MAKE_HOME_SUGGESTION ."
        echo "Abort."
        exit 0
    fi
fi

if [ -x "$OO_SDK_MAKE_HOME/make" -a -f "$OO_SDK_MAKE_HOME/make" ]
then
    makeversion=`$OO_SDK_MAKE_HOME/make --version 2> /dev/null | egrep "GNU Make[^0-9]*" | head -1 | sed -e 's#.*GNU Make[^0-9]*##' | sed -e 's#,.*##'`
    if [ "$makeversion" = "" ]
    then
        echo "The 'make' command found at $OO_SDK_MAKE_HOME/make is not GNU Make"
        echo "Set the environment variable OO_SDK_HOME to your GNU build tools directory."
        echo "GNU make version 3.79.1 can be obtained at ftp://ftp.gnu.org/gnu/make/"
        echo "Abort."
        exit 1
    fi
    testVersion $makeversion 3.79.1 $OO_SDK_MAKE_HOME/make
else
    echo "Could not find file $OO_SDK_MAKE_HOME/make ."
    echo "Abort."
    exit 0
fi


# Enter installation directory of the C++ program.
progdir=
for cppcommand in "g++" "c++" "CC"
do
    if [ -z "$progdir" ]
    then
        cppcompiler=$cppcommand
        # Search for the c++ compiler.
        progpath=`searchprog "$cppcommand"`

        # Determine directory name, and convert it to an absolute path (if necessary)
        progdir=`dirname "$progpath"`
        case "$progdir" in
            /*)	;;		# absolute path; leave unchanged
            *)	progdir=`cd "$progdir"; pwd`;;
        esac
    fi
done

OO_SDK_CPP_HOME_SUGGESTION=$progdir

echo
echo "C++ Compiler where a language binding exist:"
echo "Solaris, Sun WorkShop 6 update 1 C++ 5.2 2000/09/11 or higher"
echo "Linux, GNU C++ compiler, gcc version 3.0.1 or higher"
echon "Enter C++ tools directory: [$OO_SDK_CPP_HOME_SUGGESTION] "
read OO_SDK_CPP_HOME

if [ -z "$OO_SDK_CPP_HOME" ]
then
    if [ -d "$OO_SDK_CPP_HOME_SUGGESTION" ]
    then
        OO_SDK_CPP_HOME=$OO_SDK_CPP_HOME_SUGGESTION
    else
        echo "Error: Could not find directory $OO_SDK_CPP_HOME_SUGGESTION ."
        echo "Abort."
        exit 0
    fi
fi

if [ -x "$OO_SDK_CPP_HOME" -a -d "$OO_SDK_CPP_HOME" ]
then
    if [ $cppcompiler=g++ ]
    then
        gccversion=`$OO_SDK_CPP_HOME/$cppcompiler --version`
        testVersion $gccversion 3.0.1 $OO_SDK_CPP_HOME/$cppcompiler
    fi
else
    echo "Could not find directory $OO_SDK_CPP_HOME ."
    echo "Abort."
    exit 0
fi

# Enter installation directory of the STLPORT.
OO_STLPORT_HOME=

echo
echon "Enter STLPORT installation directory: [$TLPORT_HOME_SUGGESTION] "
read OO_STLPORT_HOME

#if [ -z "$OO_STLPORT_HOME" ]
#then
#    if [ -d "$OO_STLPORT_HOME_SUGGESTION" ]
#    then
#        OO_STLPORT_HOME=$OO_STLPORT_HOME_SUGGESTION
#    else
#        echo "Error: Could not find directory $OO_STLPORT_HOME_SUGGESTION ."
#        echo "Abort."
#        exit 0
#    fi
#fi

if [ -n "$OO_STLPORT_HOME" ]
then
    if [ -x "$OO_STLPORT_HOME" -a -d "$OO_STLPORT_HOME" ]
    then
	echo "Used STLPORT = $OO_STLPORT_HOME, version test not implemented yet."    
    else
	echo "Could not find directory $OO_STLPORT_HOME ."
	echo "Abort."
	exit 0
    fi
fi

# Enter installation directory of the JAVA program.

# Search for the program javac.
progpath=`searchprog "javac"`

# Determine directory name, and convert it to an absolute path (if necessary)
progdir=`dirname "$progpath"`
case "$progdir" in
    /*)	;;		# absolute path; leave unchanged
    *)	progdir=`cd "$progdir"; pwd`;;
esac

if [ -z "$progpath" ]
then
    progdir=
fi

if [ -d "$progdir" ]
then
    progdir=`echo "$progdir" | sed 's|/bin||'`
fi

OO_SDK_JAVA_HOME_SUGGESTION=$progdir

echo
echon "Enter JAVA (1.3.1_02 or higher) tools directory: [$OO_SDK_JAVA_HOME_SUGGESTION] "
read OO_SDK_JAVA_HOME

if [ -z "$OO_SDK_JAVA_HOME" ]
then
    if [ -d "$OO_SDK_JAVA_HOME_SUGGESTION" ]
    then
        OO_SDK_JAVA_HOME=$OO_SDK_JAVA_HOME_SUGGESTION
    else
        echo "Error: Could not find directory $OO_SDK_JAVA_HOME_SUGGESTION ."
        echo "Abort."
        exit 0
    fi
fi

if [ -x "$OO_SDK_JAVA_HOME" -a -d "$OO_SDK_JAVA_HOME" ]
then
    javaversion=`$OO_SDK_JAVA_HOME/bin/java -version 2>&1 | egrep "java version" | head -1 | sed -e 's#.*version "##' | sed -e 's#".*##'`
    testVersion $javaversion 1.3.1_02 $OO_SDK_JAVA_HOME/bin/java
else
    echo "Could not find directory $OO_SDK_JAVA_HOME ."
    echo "Abort."
    exit 0
fi


# Enter installation directory of the ANT program.

# Search for the program ant.
progpath=`searchprog "ant"`

# Determine directory name, and convert it to an absolute path (if necessary)
progdir=`dirname "$progpath"`
case "$progdir" in
    /*)	;;		# absolute path; leave unchanged
    *)	progdir=`cd "$progdir"; pwd`;;
esac

if [ -z "$progpath" ]
then
    progdir=
fi

if [ -d "$progdir" ]
then
    progdir=`echo "$progdir" | sed 's|/bin||'`
fi

OO_SDK_ANT_HOME_SUGGESTION=$progdir

echo
echon "Enter ANT (1.4 or higher) installation directory (optional): [$OO_SDK_ANT_HOME_SUGGESTION] "
read OO_SDK_ANT_HOME

if [ -z "$OO_SDK_ANT_HOME" ]
then
    if [ -d "$OO_SDK_ANT_HOME_SUGGESTION" ]
    then
        OO_SDK_ANT_HOME=$OO_SDK_ANT_HOME_SUGGESTION
    fi
fi

if [ -x "$OO_SDK_ANT_HOME" -a -d "$OO_SDK_ANT_HOME" ]
then
    ANT_HOME=$OO_SDK_ANT_HOME
    export ANT_HOME
    antversion=`$OO_SDK_ANT_HOME/bin/ant -version | egrep "Ant version" | head -1 | sed -e 's#Ant version ##' | sed -e 's# .*##'`
    testVersion $antversion 1.4 $OO_SDK_ANT_HOME/bin/ant
else
    if [ $OO_SDK_ANT_HOME ]
    then
        echo "Could not find directory $OO_SDK_ANT_HOME ."
        echo "Abort."
        exit 0
    fi
fi


# Automatic deployment?
SDK_AUTO_DEPLOYMENT_SUGGESTION="YES"

echo
echon "Automatic deployment (YES/NO): [$SDK_AUTO_DEPLOYMENT_SUGGESTION] "
read SDK_AUTO_DEPLOYMENT

if [ -z "$SDK_AUTO_DEPLOYMENT" ]
then
    SDK_AUTO_DEPLOYMENT=$SDK_AUTO_DEPLOYMENT_SUGGESTION
else
    if [ "$SDK_AUTO_DEPLOYMENT" != "YES" -a "$SDK_AUTO_DEPLOYMENT" != "NO" ]
    then
        echo "Error: The value entered \"$SDK_AUTO_DEPLOYMENT\" is invalid."
        echo "Abort."
        exit 0
    fi
fi


# Copy the template and insert the variables.
if [ $OO_SDK_ANT_HOME ]
then
    sed -e "s|@OO_SDK_HOME@|$OO_SDK_HOME|g" -e "s|@OFFICE_HOME@|$OFFICE_HOME|g" -e "s|@OO_SDK_MAKE_HOME@|$OO_SDK_MAKE_HOME|g" -e "s|@OO_SDK_CPP_HOME@|$OO_SDK_CPP_HOME|g" -e "s|@OO_SDK_JAVA_HOME@|$OO_SDK_JAVA_HOME|g" -e "s|@SDK_AUTO_DEPLOYMENT@|$SDK_AUTO_DEPLOYMENT|g" -e "s|@OO_STLPORT_HOME@|$OO_STLPORT_HOME|g" -e "s|@OO_SDK_ANT_HOME@|$OO_SDK_ANT_HOME|g" < setsdkenv_unix.in > setsdkenv_unix
else
    sed -e "s|@OO_SDK_HOME@|$OO_SDK_HOME|g" -e "s|@OFFICE_HOME@|$OFFICE_HOME|g" -e "s|@OO_SDK_MAKE_HOME@|$OO_SDK_MAKE_HOME|g" -e "s|@OO_SDK_CPP_HOME@|$OO_SDK_CPP_HOME|g" -e "s|@OO_SDK_JAVA_HOME@|$OO_SDK_JAVA_HOME|g" -e "s|@SDK_AUTO_DEPLOYMENT@|$SDK_AUTO_DEPLOYMENT|g" -e "s|@OO_STLPORT_HOME@|$OO_STLPORT_HOME|g" -e "s|@OO_SDK_ANT_HOME@||g" < setsdkenv_unix.in > setsdkenv_unix
fi

chmod a+x ./setsdkenv_unix
