#!/bin/sh
#
# Copyright 1998-2001 Microsoft Corporation -- All Rights Reserved.
#

main() {
    initialize                          # setup environment
    checkuser      || error             # Check to make sure we are root
    banner         || error             # Display header/copyright info
    
    if [ ! -r $FPDIR ]
    then
        echo "The FrontPage Server Extensions do not appear to be installed."
        echo "Please install FPSE, then rerun this script."
        exit 1;
    fi
    
    untarext       || error             # untar the files
    $FPDIR/set_default_perms.sh         # Run the external permissions script.
    
    echo 
    echo "Installation completed!  Exiting..." 
    
    exit 0
}

initialize()
{
    VERSION="5.0"
    PATH=".:/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb:/etc:/usr/bsd"
    FPDIR="/usr/local/frontpage/version${VERSION}"
    
    case "`echo 'x\c'`" in
       'x\c')   echo="echo -n"    nnl= ;;      #BSD
       x)       echo="echo"       nnl="\c" ;;  # Sys V
       *)       echo "$0 quitting:  Can't set up echo." ; exit 1 ;;
    esac
}

#
# The following functions do some initial checks that we are who we need to be
# and have the directories and permissions on the directories that we need
#
checkuser()
{
    # Make sure we are running as root.
    
    whoami=`whoami 2>/dev/null` || whoami=`/usr/bin/id | sed -e ' s/).*//; s/^.*(//;'`
    retval=0
    
    echo "Setting umask 002" 
    umask 002
    
    if [ $whoami != "root" ]
    then
        echo "ERROR:  Logged in as $whoami.  Must be root to do this installation!"
        retval=1
    else
        echo "Logged in as root." 
    fi
    
    return $retval
}

banner()
{
cat <<EOF

 fp_install_intl.sh
 
This script will step the user through installation of the FrontPage Server
Extensions International package.  As with any software installation, a backup
should be done before continuing.  It is recommended that the FrontPage
installation directory, server configuration file directory, and all web
content be backed up before continuing with this installation.

EOF
    
   myprompt 'yYnN' "Are you satisfied with your backup of the system (y/n)" "N"
   if [ $answer = n ] || [ $answer = N ]
   then
      exit 0
   fi
   echo
   
   return 0
}

# Untar the extensions into the installation directory
untarext()
{
    retval=0
    
    echo "What language international package would you like to install (3 letter code)?"
    read lang
    
    vtfile="fp50_$lang.tar"
    vtfilelocation="`pwd`/"
    
    getextfilename $vtfilelocation $vtfile || return 1
    
    olddir=`pwd`
    
    cd /usr/local
    
    case $fullvtfile in 
     *tar) echo "Untarring file $fullvtfile into /usr/local..." 
           tar -xf $fullvtfile ;;
       *Z) echo "Uncompressing/Untarring file $fullvtfile into /usr/local..."
           zcat $fullvtfile | tar -xf - 
           ;;
      *gz) zcat=""
           while [ "$zcat" = "" ] || [ ! -f "$zcat" ]
           do
               $echo "Where is the zcat which can uncompress gz files? ${nnl}"
               read zcat
               base=`basename zcat`
               case $base in
                  zcat) ;;
                     *) zcat=`dirname $zcat`/$base/zcat ;;
               esac
           done
           echo "Uncompressing/Untarring file $fullvtfile into /usr/local..."
           $zcat $fullvtfile | tar -xf -
           ;;
        *) echo "ERROR:  Unknown file type: $fullvtfile"
           return 1
           ;;
    esac
    
    cd $olddir
    
    return $retval
}

# get the name of the tarball file
# used by untarext
getextfilename()
{
    vtfilelocation=$1
    vtfile=$2
    fullvtfile="${vtfilelocation}${vtfile}"
    if [ ! -f "$fullvtfile" ]
    then
        if [ -f "${fullvtfile}.Z" ]
        then
            fullvtfile="${fullvtfile}.Z"       
        else
            if [ -f "${fullvtfile}.gz" ]
            then
                fullvtfile="${fullvtfile}.gz"
            else
                if [ -f "${fullvtfile}.z" ]
                then
                    echo "Renaming ${vtfile}.z to ${vtfile}.Z"
                    mv "${fullvtfile}.z" "${fullvtfile}.Z"
                    fullvtfile="${fullvtfile}.Z"
                else
                    echo "Cannot find ${vtfile} in ${vtfilelocation}." 
                    $echo "Which directory is the file located in (X to cancel)?  ${nnl}" 
                    read vtfilelocation
                    if [ "$vtfilelocation" = "X" ] || [ "$vtfilelocation" = "x" ]
                    then
                        return 1
                    else
                        vtfilelocation=`echo $vtfilelocation |sed -e 's/\/$//'`
                        
                        if [ ! -d "$vtfilelocation" ]
                        then
                            vtfilelocation=`dirname $vtfilelocation`
                        fi
                        getextfilename $vtfilelocation/ $vtfile
                    fi
                fi
            fi
        fi
    fi
}


################################################################################
# basic utility functions
#

myprompt()
{
    answer="|"
    until echo $1 | grep $answer >/dev/null
    do
        $echo "${2} [${3}]?  ${nnl}" 
        read answer
        if [ "$3" != "" ] && [ "$answer" = "" ]
        then
            answer=$3
        fi
    done
}

# Print an error message and exit the program.
error()
{
    echo 
    echo "Exiting due to an error!  Please fix the error and try again." 
    echo 
    
    exit 1
}

main
