#!/bin/bash

function InitProfile()
{
  exec 2>&1				# redirect stderr

  SetupWine		|| return 1
  SetupWineTweaks	|| return 1
  SetupEnv		|| return 1

  echo "ok"
}

function SetupWine()
{
  # setup dosdevices and symlinks
  local c_sym="$WINEPREFIX/dosdevices/c:"
  local c_dir="$WINEPREFIX/drive_c/"
  local z_sym="$WINEPREFIX/dosdevices/z:"
  local z_dir="/"
  
  make_path "$WINEPREFIX/dosdevices"	&&
  setup_drive_symlink "$c_sym" "$c_dir"	&&
  setup_drive_symlink "$z_sym" "$z_dir"	&&

  # setup program files and logfile symlinks
  setup_prog_dir			&&
  setup_temp_dir			&&
  setup_winemenubuilder
}


# setup/validate drive symlinks
function setup_drive_symlink()
{
  local sym="$1"
  local dst="$2"

  if ! ( [ -h "$sym" ] && [ -d "$sym" ] ) ; then	# symlink to dir
    rm -f "$sym"
  fi
  [ -d "$sym" ] || ln -s "$dst" "$sym" || fail "Could not create $sym (link to $dst)"
}

# setup temp dir
function setup_temp_dir
{
  local cuser=$(id -un)
  local basepath="$WINEPREFIX/drive_c/users/$cuser"
  
  mkdir -p "$basepath/temp" # needed?
}

# TODO simplify by using a different Log dir
function setup_prog_dir()
{
  local progdir="$WINEPREFIX/drive_c/TeamViewer"
  local progsrc="$TV_WINE_DIR/drive_c/TeamViewer"
  local name
  local dst

  if ! [ -d "$progdir" ] ; then
    mkdir -p "$progdir" || fail "Could not create $progdir"
  fi

  # always check all files (may change due to updates)
  for item in "$progsrc"/* ; do
    name=${item##/*/}		# aka basename
    dst="$progdir/$name"

    if ! ( [ -h "$dst" ] && [ -f "$dst" ] ) ; then	# symlink to file
      rm -f "$dst"
      ln -s "$item" "$dst" || die "Could not create $dst (link to $item)"
    fi
  done
}

function setup_winemenubuilder()
{
  local sysdir="$WINEPREFIX/drive_c/windows/system32"
  local syssrc="$TV_WINE_DIR/drive_c/windows/system32"

  mkdir -p "$sysdir"
  cp "$syssrc/winemenubuilder.exe" "$sysdir/winemenubuilder.exe"
  [ -e "$sysdir/winemenubuilder.exe" ] || die "Could not copy winemenubuilder"
}

function SetupWineTweaks()
{
  [ "$USE_LOCAL_WINE" = "yes" ] && return
  [ -d "$WINEPREFIX/.tweak"   ] || mkdir "$WINEPREFIX/.tweak"

  RequireWineServer		# make sure wineserver is running (QS)
  
  winetweak fontsmooth_rgb	# Enable Subpixel Hinting
  winetweak no_xvidmodeext	# Disable XVidExtension (causes problems with some drivers)
  winetweak no_xrandr		# Disable XRandR (causes problems with some drivers)
  winetweak no_fileassocs	# Disable synchronizing of file associations and menu entries
#  winetweak setwinver		# Set Win2k mode for TeamViewer
}

function winetweak()
{
  local tweak="$1"
  local tweakmark="$WINEPREFIX/.tweak/$tweak"
  local tweakfile="$TV_SCRIPT_DIR/$tweak.reg"
  
  [ -e "$tweakmark" ] && return

  wine regedit "$tweakfile" && touch "$tweakmark"
}

function SetupEnv()
{
echo " - "

  make_path        "$TV_CFG_DIR" 0700	|| return 1

  # identify architecture
  uname -m > "$WINEPREFIX/drive_c/distarch"
  (cd /etc; ls -m *-release *-version *_version > "$WINEPREFIX/drive_c/distrelease" 2> /dev/null)
  true
}
