export WINEDEBUG="fixme-all"
SETARCH=""
WINE=""
WINEDIR="`echo ~/.wine`"
DIALOG="/usr/share/winetools/Xdialog"
BASEDIR="/usr/share/winetools"
#export PATH="$BASEDIR:$PATH"
BINDIR="$HOME/bin"
CDROM="`ls -l $HOME/.wine/dosdevices/ 2>/dev/null | grep \" d: -> \"| sed -e \"s/^.* d: -> \(.*\)$/\1/\"`"
START="T"
PARAM="$1"
export TEXTDOMAINDIR="/usr/share/locale"
export TEXTDOMAIN="wt0.9"

#for Debian:
#BASEDIR="/usr/share/winetools"
#DIALOG="/usr/bin/Xdialog"
#export TEXTDOMAINDIR=/usr/share/locale

# find gettext.sh
. gettext.sh &> /dev/null
if [ "$?" != "0" ]; then
  $DIALOG	--title "*** WARNING ***" \
		--no-cancel \
                --msgbox "You have no gettext package installed or\ngettext.sh is not in your path. You will not get\nany internationalized messages from WineTools." 9 60
  . $BASEDIR/gettext.sh.dummy
fi

test_oss()
{
  return 0
}

mount_cd()
{
  mounted="false"
  $DIALOG --title "`eval_gettext "*** NOTE ***"`" \
	--yesno "`eval_gettext "For this software you need a mounted CD/DVD. If your CD/DVD is\nalready mounted or your system uses Supermount, say \"no\" here.\nAlso note that some software needs the the CD/DVD to be mounted\nwith the unhide option. Shall I mount it for you?"`" 9 60
  if [ "$?" = "0" ]; then
    mount "$CDROM"
    if [ "$?" != "0" ]; then
      $DIALOG --title "`eval_gettext "*** ERROR ***"`" \
              --no-cancel \
              --msgbox "`eval_gettext "Mounting of the CD failed. You should do it manually."`" 7 60
    else
      mounted="true"
    fi
  fi
}

umount_cd()
{
  if [ "$mounted" = "true" ]; then
    mounted="false"
    $DIALOG --title "`eval_gettext "*** NOTE ***"`" \
	--yesno "`eval_gettext "Shall I unmount the CD for you?"`" 7 60
    if [ "$?" = "0" ]; then
      umount "$CDROM"
      [ "$?" != "0" ] && $DIALOG --title "`eval_gettext "*** ERROR ***"`" \
			         --no-cancel \
			         --msgbox "`eval_gettext "Unmounting of the CD failed. You should do it manually."`" 7 60
    fi
  fi
}

root_test()
{
  ID="`id -u`"
  ID2="`id -ur`"
  if [ "$ID" = "0" ] || [ "$ID2" = "0" ]; then
    echo "WineTools can not be used as root."
    exit 1
  fi
}

root_test

# test for ALSA and OSS compatibility layer
#test_oss && [ -e "/usr/lib/libaoss.so" ] && export LD_PRELOAD=/usr/lib/libaoss.so && echo "Using OSS-ALSA conversion library."
#echo "LD_PRELOAD=$LD_PRELOAD"

# find out which wine we are using
if [ "`which wine 2>/dev/null`" = "" ]; then
  $DIALOG  --title "`eval_gettext "*** NOTICE ***"`" \
           --msgbox "`eval_gettext "You don't have Wine installed on your computer. Go to http://www.winehq.org\nand download an appropiate version for your distribution!"`" 8 70
  exit 1
fi

# winecfg is needed
if [ "`which winecfg 2>/dev/null`" = "" ]; then
  $DIALOG  --title "`eval_gettext "*** NOTICE ***"`" \
           --msgbox "`eval_gettext "winecfg was not found. If you have a Debian based\ndistribution it is necessary to install the package wine-utils!"`" 8 70
  exit 1
fi

if [ ! -e "$WINEDIR/drive_c/windows/regedit.exe" ]; then
  echo "`eval_gettext "no suitable Wine directory found..."`"
  $DIALOG  --title "`eval_gettext "*** NOTICE ***"`" \
           --msgbox "`eval_gettext "Wine has not been configured for this user yet. Start\nWineTools with \"wt\" on a command line and go to \"Base\ setup\" first.\nGo through all sub menu entries before you install other software."`" 8 60
  START="F"
  if [ "$WINEVER" = "" ]; then
    mkdir /tmp/wine-$$
    touch /tmp/wine-$$/.no_debug_window_flag
    touch /tmp/wine-$$/.no_prelaunch_window_flag
    WINEVER="`WINEPREFIX=/tmp/wine-$$ wine --version 2>&1 | grep "Wine [0-9]" | sed -e \"s/Wine //\"`"
    rm -Rf /tmp/wine-$$
  fi
else
  # find out the Wine version if WINEVER is not set in environment
  echo -n "`eval_gettext "detecting Wine version... "`"
  [ "$WINEVER" = "" ] && WINEVER="`wine --version 2>&1 | grep "Wine [0-9]" | sed -e \"s/Wine //\"`"

  echo "done."

  # finding drive C:
  DRIVEC=""
  [ -d "$WINEDIR/drive_c" ] && [ ! -h "$WINEDIR/drive_c" ] && { DRIVEC="$WINEDIR/drive_c";others="c fake_windows"; }
  [ -d "$WINEDIR/c" ] && [ ! -h "$WINEDIR/c" ] && { DRIVEC="$WINEDIR/c";others="drive_c fake_windows"; }
  [ -d "$WINEDIR/fake_windows" ] && [ ! -h "$WINEDIR/fake_windows" ] && { DRIVEC="$WINEDIR/fake_windows";others="c drive_c"; }
  [ "$DRIVEC" = "" ] && echo "`eval_gettext "no fake drive C: found!"`"
  [ "$DRIVEC" = "" ] || echo "`eval_gettext "Drive C: is $DRIVEC"`"

  # make windows pathnames out of parameters if it's not an URI with ftp|http[s]
  if [ "$PARAM" != "" ] && [ "$PARAM" != "--noexit" ]; then
    PARAM="`echo \"$1\"|grep -e \"\(^ *http[s]\?://\)\|\(^ *ftp://\)\" 2>/dev/null`"
    [ "$PARAM"  = "" ] && PARAM="`winepath -l \"$1\" 2>/dev/null`"
  fi

  # many applications want to be started from the fake windows drive
  cd "$DRIVEC"
fi

# find out whether we have to avoid exec-shield
grep "Fedora Core" /etc/redhat-release &>/dev/null
[ "$?" = "0" ] && SETARCH="setarch i386 "
[ "`which setarch 2>/dev/null`" = "" ] && SETARCH=""
WINE="${SETARCH}wine"

# set WINEVER to none if not yet set
[ "$WINEVER" = "" ] && WINEVER="0"

# say what we have done
echo "Wine $WINEVER"
echo "`eval_gettext "wine is executed as"`" "$WINE"
echo "`eval_gettext "Parameters are"`" "$PARAM"

if [ "$START" = "F" ]; then
  echo "`eval_gettext "Wine is not configured yet!"`"
  if [ "$1" != "--noexit" ]; then
    exit 1
  fi
fi
