#!/bin/sh
# Copyright (c) 2001-2004, TransGaming Technologies Inc.
# Startup script for Cedega.
# Version 1.9.1a Feb 16th 2004

usage()
{
	echo "usage: cedega [-bigexe] [-use-pthreads <yes/no>] [[-]-winver <version>] [[-]-debugmsg <debug>] [[-]-version] [[-]-use-dos-cwd <dir>] <application> [application parameters]"
    exit 1
}


# Set up the installdir first (to make it easily movable to /opt/)
if [ -z "$WINEXINSTALLDIR" ]
then
  export WINEXINSTALLDIR=/usr/lib/transgaming_cedega/
fi

# Set up the WINEPREFIX
if [ -z "$WINEPREFIX" ]
then
  export WINEPREFIX="$HOME/.transgaming"
fi

# Set up the WINEXFONTPREFIX
if [ -z "$WINEXFONTPREFIX" ]
then
  export WINEXGLOBALPREFIX="$HOME/.transgaming_global"
fi

# Setup LD_LIBRARY_PATH
export LD_LIBRARY_PATH="$WINEXINSTALLDIR/winex/lib:$WINEXINSTALLDIR/winex/bin:$LD_LIBRARY_PATH"

# Set up the path so that ancilary apps (like the debugger) can start
export PATH="$WINEXINSTALLDIR/winex/bin:$PATH"

# Set the WINESERVER variable so we start the right one
export WINESERVER="$WINEXINSTALLDIR/winex/bin/wineserver"

# Set the WINE_LOADER variable so that wineshelllink knows how to run cedega
export WINE_LOADER=cedega

# Set the WINESHELLLINK variable to point at where it should be installed
export WINESHELLLINK="$WINEXINSTALLDIR/winex/bin/wineshelllink"

# Set the WINEDBG variable to point at where it is installed
export WINEDBG="$WINEXINSTALLDIR/winex/bin/winedbg"


RUNWINENORMAL="$WINEXINSTALLDIR/winex/bin/wine" 
RUNWINERELOCATED="$WINEXINSTALLDIR/winex/bin/wine_relocated" 
RUNWINE=$RUNWINENORMAL
REGAPI="$WINEXINSTALLDIR/winex/bin/regapi" 

# The following section deals with the command-line.


FULL_COMMAND_LINE=""
COMMAND_LINE=""
DOUBLEDASH=""
VERSION=""
DEBUGMSG="-all"
WINVER=""
WORKDIR="."

while : ; do
    case $# in 0) break ;; esac
      case "$1" in
        "-bigexe" )
           RUNWINE=$RUNWINERELOCATED
	   shift
           ;;
        "--") 
	   DOUBLEDASH=yes;
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE \"$1\""
	   shift
	   ;;
        "-version" | "--version" )
	   VERSION=yes
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE \"$1\""
	   shift
           ;;
	"-winver" | "--winver" )
	   FULL_COMMAND_LINE="$FULL_COMMAND_LINE \"$1\""
	   WINVER="-winver $2"
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE \"$2\""
	   if ! shift 2; then usage; fi
	   ;;
	"-debugmsg" | "--debugmsg" )
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE \"$1\""
	   DEBUGMSG="\"$2\""
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE $DEBUGMSG"
	   if ! shift 2; then usage; fi
           ;;
        # NOTE: That workdir has been replaced with -use-dos-cwd
        "-workdir" | "--use-dos-cwd" | "-use-dos-cwd" )
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE \"$1\""
           WORKDIR="\"$2\""
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE $WORKDIR"
           if ! shift 2; then usage; fi
           ;;

        "-use-pthreads" )
          USE_PTHREADS=$2
	  if ! shift 2; then usage; fi
          ;;

        * )
	   COMMAND_LINE="$COMMAND_LINE \"$1\"" 
           FULL_COMMAND_LINE="$FULL_COMMAND_LINE \"$1\""
	   shift
           ;;
    esac
done

# Ensure that all child processes will run the correct binary
export WINELOADER="$RUNWINE"

# Test the commandline and stacksize to see if we will be using pthreads or not.
# We need at least 6 megabytes to use pthreads safely but command line overrides
case $USE_PTHREADS in
   "yes" )
     export LD_LIBRARY_PATH="$WINEXINSTALLDIR/winex/pthread_lib:$LD_LIBRARY_PATH"
     ;;
   "no" )
     ;;
   * )
     WINEX_SYSTEM_PTHREAD_SIZE=`$WINEXINSTALLDIR/winex/bin/pthreads_stack_test | sed 's/^MAXIMUM SUPPORTED STACKSIZE: //g'`
     if [ $WINEX_SYSTEM_PTHREAD_SIZE -gt 0 ]
     then
       # This setup requires pthreads, but doesn't seem to provide a reasonable enough stack. Warn the user
       # as some games may not work as a result, but allow them to continue because some chance is better than none.
       # SuSE 8.2 and older redhat releases, among others, have this issue.
       if [ $WINEX_SYSTEM_PTHREAD_SIZE -lt 6144 -a -z "$WINEX_PTHREADS_WARNING_ISSUED" ]
       then
         echo -n "Your system requires the use of pthreads but the maximum system allowed stack size " 1>&2
         echo    "of $WINEX_SYSTEM_PTHREAD_SIZE kB may be too small for some games." 1>&2
         echo    "If you experience problems, try rerunning with \"-use-pthreads no\" which may help." 1>&2
         export WINEX_PTHREADS_WARNING_ISSUED=yes
       fi
       export LD_LIBRARY_PATH="$WINEXINSTALLDIR/winex/pthread_lib:$LD_LIBRARY_PATH"
     fi
     ;;
  
esac

update_fonts()
{
    # If the windows/Fonts is a directory this is a pre WineX 3.3 install and needs to be
    # converted.
    if [ -d "$WINEPREFIX/c_drive/windows/Fonts" -a ! -h "$WINEPREFIX/c_drive/windows/Fonts" ]
    then
      echo "Moving all local fonts to $WINEXGLOBALPREFIX/Fonts and removing local Fonts directory"
      mv    "$WINEPREFIX/c_drive/windows/Fonts"/* "$WINEXGLOBALPREFIX/Fonts/" 
      rmdir "$WINEPREFIX/c_drive/windows/Fonts"
      ln -s "$WINEXGLOBALPREFIX/Fonts" "$WINEPREFIX/c_drive/windows/Fonts"
    fi

    # See if there are any TransGaming fonts which need updating
    TG_FONT_VERSION=`cat "$WINEXGLOBALPREFIX/Fonts/tg_font_version"`
    if [ $TG_FONT_VERSION -lt $CUR_CONFIG_VERSION ]
    then
       cp -R "$WINEXINSTALLDIR/.transgaming/c_drive/windows/Fonts"/* "$WINEXGLOBALPREFIX/Fonts/"
       echo $CUR_CONFIG_VERSION > "$WINEXGLOBALPREFIX/Fonts/tg_font_version"
    fi
}


# Establish the current config version
CUR_CONFIG_VERSION=43


# Does the global resource directory exist yet?
if !([ -e "$WINEXGLOBALPREFIX" ])
then
    # Create the directory 
    mkdir "$WINEXGLOBALPREFIX"
    mkdir "$WINEXGLOBALPREFIX/Fonts"
    echo 0 > "$WINEXGLOBALPREFIX/Fonts/tg_font_version"
fi

# If we're being run for the first time:
if !([ -e "$WINEPREFIX" ]); then

# Copy the .transgaming directory into the user's home dir

    cp -r "$WINEXINSTALLDIR/.transgaming" "$WINEPREFIX"

# Ensure that user has appropriate permissions for all subdirs

    chmod -R u+w "$WINEPREFIX"

# Set C drive directly appropriately
    WINEPREFIX_FILTERED=`echo $WINEPREFIX | sed 's|\&|\\\&|g'`
    sed "s,@C_DRIVE@,$WINEPREFIX_FILTERED/c_drive,g" < "$WINEXINSTALLDIR/.transgaming/config" > "$WINEPREFIX/config"

# Link the c_drive to the user's home
    if [ ! -d "$HOME/TransGaming_Drive" ]; then
        ln -s "$WINEPREFIX/c_drive" "${HOME}/TransGaming_Drive"
    fi

# Link the regsvr32 applet into the windows system directory
    if [ ! -e "$WINEPREFIX/c_drive/windows/system32/regsvr32" ]; then
        ln -s "$WINEXINSTALLDIR/winex/bin/regsvr32" "$WINEPREFIX/c_drive/windows/system32/regsvr32"
        ln -s "$WINEXINSTALLDIR/winex/bin/regsvr32.so" "$WINEPREFIX/c_drive/windows/system32/regsvr32.so"
    fi

# Link winebrowserlink into the windows system directory
    if [ ! -e "$WINEPREFIX/c_drive/windows/system32/winebrowserlink" ]; then
        ln -s "$WINEXINSTALLDIR/winex/bin/winebrowserlink" "$WINEPREFIX/c_drive/windows/system32/winebrowserlink"
        ln -s "$WINEXINSTALLDIR/winex/bin/winebrowserlink.so" "$WINEPREFIX/c_drive/windows/system32/winebrowserlink.so"
    fi

# And now update the Fonts
    update_fonts

# Now update the registry keys
    "$REGAPI" -- setValue -force < "$WINEXINSTALLDIR/update.reg"  2>&1 > /dev/null
 
fi


# Get the version of the config files / registry
if [ -e "$WINEPREFIX/tg_config_version" ]; then
    TG_CONFIG_VERSION=`cat "$WINEPREFIX/tg_config_version"`
else
    TG_CONFIG_VERSION=1
fi

# Check if we need to update the config
if [ $TG_CONFIG_VERSION -lt $CUR_CONFIG_VERSION ]; then

# We need to update.  Inform the user of the update
    echo "Updating TransGaming config and registry info"
    echo "WARNING: Your old config file has been moved to config.old. Any modifications you made to this file"
    echo "will have to be replaced."
                                                               
# First copy in the new config file.  Save the old one first and fixup C drive in config file.
    mv "$WINEPREFIX/config" "$WINEPREFIX/config.old"  

    WINEPREFIX_FILTERED=`echo $WINEPREFIX | sed 's|\&|\\\&|g'`
    sed "s,@C_DRIVE@,$WINEPREFIX_FILTERED/c_drive,g" < "$WINEXINSTALLDIR/.transgaming/config" > "$WINEPREFIX/config"

# Now the dynamic registry data
    cp "$WINEXINSTALLDIR/.transgaming/dyndata.reg" "$WINEPREFIX/dyndata.reg"

# Now update the registry keys
    "$REGAPI" -- setValue -force < "$WINEXINSTALLDIR/update.reg"  2>&1 > /dev/null

# Create a 'My Documents' directory, if it doesn't already exist
    if [ ! -d "$WINEPREFIX/c_drive/My Documents" ];  then
      mkdir "$WINEPREFIX/c_drive/My Documents"
    fi

# And new things for the windows directory
    cp -R "$WINEXINSTALLDIR/.transgaming/c_drive/windows/system32" "$WINEPREFIX/c_drive/windows/"

# Link the regsvr32 applet into the windows system directory. Must be done every upgrade.
    if [ -e "$WINEXINSTALLDIR//winex/bin/regsvr32" ]; then
        rm -f "$WINEPREFIX/c_drive/windows/system32/regsvr32" "$WINEPREFIX/c_drive/windows/system32/regsvr32.so"
    fi

    ln -s "$WINEXINSTALLDIR/winex/bin/regsvr32" "$WINEPREFIX/c_drive/windows/system32/regsvr32"
    ln -s "$WINEXINSTALLDIR/winex/bin/regsvr32.so" "$WINEPREFIX/c_drive/windows/system32/regsvr32.so"
                                                                                                                                
# Link winebrowserlink into the windows system directory. Must be done every upgrade.
    if [ -e "$WINEXINSTALLDIR/winex/bin/winebrowserlink" ]; then
        rm -f "$WINEPREFIX/c_drive/windows/system32/winebrowserlink" "$WINEPREFIX/c_drive/windows/system32/winebrowserlink.so"
    fi

    ln -s "$WINEXINSTALLDIR/winex/bin/winebrowserlink" "$WINEPREFIX/c_drive/windows/system32/winebrowserlink"
    ln -s "$WINEXINSTALLDIR/winex/bin/winebrowserlink.so" "$WINEPREFIX/c_drive/windows/system32/winebrowserlink.so"

# Write out the new version number
    cp "$WINEXINSTALLDIR/.transgaming/tg_config_version" "$WINEPREFIX/tg_config_version" 

# And now update the Fonts
    update_fonts

    echo "Update complete"
fi


# Every time

# Force the use of the more memory-efficient MSVC 5 allocator with
# any programs that use MSVCRT.  This reduces memory consumption
# significantly.
export __MSVCRT_HEAP_SELECT=__GLOBAL_HEAP_SELECTED,2


# If no arguments after stripping -bigexe, report and error
if [ -z "$FULL_COMMAND_LINE" ]
then 
  usage
fi

# It is possible that the user has their system misconfigured somehow
# and doesn't have the $SHELL script set appropriately. If that is
# the case, default to the standard 'sh' shell and output a warning
if [ -z $SHELL ]; then
   echo -n "No \$SHELL environment variable set. Defaulting to "
   echo `which sh`
   export SHELL=`which sh`  
fi;

# If "--" appears as an argument, then we will not do any more processing on the
# command line, but just pass it directly to RUNWINE.

if [ -n "$DOUBLEDASH" ]; then
    $SHELL -c "$RUNWINE $FULL_COMMAND_LINE"
else
    if [ -n "$VERSION" ]; then
        exec $RUNWINE -version
    else
        if [ -z "$COMMAND_LINE" ]; then usage; fi;


        $SHELL -c "$RUNWINE $WINVER -debugmsg $DEBUGMSG -use-dos-cwd $WORKDIR -- $COMMAND_LINE"
    fi;
fi;
