#!/bin/bash
#       wrapper
#   Script to set up the Picasa environment and launch an executable
#   Invoke with WINEDEBUG set to enable debug messages.

# Compute a directory 
mydir=`dirname $0`

echo $mydir | grep -q '^/'
if [ $? -ne 0 ] ; then
    dir0=`dirname $0 | sed 's!^\.$!!'`
    mydir=`pwd`/$dir0
fi

warn_user() {

    # First, send it to stdout for the power users who like looking at the console

    echo "$@"

    # Then send it to a GUI thingy for everyone else
    MESSAGE=true
    type xmessage > /dev/null 2>&1 && MESSAGE=xmessage
    type gmessage > /dev/null 2>&1 && MESSAGE=gmessage
    type kmessage > /dev/null 2>&1 && MESSAGE=kmessage

    $MESSAGE -center -buttons "    Continue    ":7,"    Quit    ":8 -title "Warning" "$@"
    case $? in
        7)  ;;         # user picked 'Continue'
    	8)  exit 1 ;;  # user picked 'Quit'
    esac
}

# This test is to work around a problem with libGL provided by NVidia.
if test -e /dev/nvidia0; then
    if test ! -w /dev/nvidia0 -o ! -r /dev/nvidia0 -o \
           ! -w /dev/nvidiactl -o ! -r /dev/nvidiactl; then
        warn_user "
/dev/nvidia0 or /dev/nvidiactl are not accessable.  Picasa will crash
if these files are not accessable. To fix this, as root, please run:

chmod 666 /dev/nvidia0 /dev/nvidiactl

and then restart this program."
        exit 1
    fi
fi


# Set the environment up
export PATH=$mydir/../wine/bin:$mydir:$PATH
export LD_LIBRARY_PATH=$mydir/../wine/lib:$mydir/../lib:$LD_LIBRARY_PATH
export WINEDLLPATH=$mydir/../wine/lib/wine
export WINELOADER=$mydir/../wine/bin/wine
export WINESERVER=$mydir/../wine/bin/wineserver
export WINEPREFIX=$HOME/.picasa

if [ -n "$PICASA_WINEPREFIX" ] ; then
     # User specified a prefix in the environment, use it
     export WINEPREFIX="$PICASA_WINEPREFIX"
elif [ ! -d "$WINEPREFIX" ] ; then
    fstype=`stat --filesystem --format %T --dereference $HOME 2>/dev/null`
    if [ "$fstype" = "nfs" ] ; then
        # Home directory is on a remote volume; that can be very slow.
        # The user can set PICASA_WINEPREFIX to point to a local dir to avoid this.
        # Alternately, since environment variables like that are not easy to set sitewide,
        # sysadmins can create a direct subdirectory of /usr/local with permissions 1777.
        LOCALDIR=/var/opt/picasa

        # Super special exception - if we find a directory under /usr/local that is set
        #   up with 1777 permissions, let's presume that it was set up for this purpose, and use it
        if [ ! -d "$LOCALDIR" ] ; then
            LOCALDIR=`find /usr/local -maxdepth 1 -type d -perm 1777 2>/dev/null | head -1`
        fi

        ALTPREFIX="$LOCALDIR/$LOGNAME/picasa.dir"
        [ -d $ALTPREFIX ] || ( [ -d "$LOCALDIR" ] && mkdir -p $ALTPREFIX 2>/dev/null && touch $ALTPREFIX/.here)
        if [ -f $ALTPREFIX/.here ] ; then
            # Leave a symlink behind.  
            ln -sf "$ALTPREFIX" "$WINEPREFIX"
            # but don't use it for this run, go straight to the symlink's target
            WINEPREFIX=$ALTPREFIX
        else
            warn_user "Performance may be poor if ~/.picasa is not on a local drive. 
You can work around this in several ways:
- by making ~/.picasa a symlink to a directory on a local drive,
- by setting the environment variable PICASA_WINEPREFIX, or 
- by creating a directory /var/opt/picasa/$LOGNAME/ which you have rights to
  (in which case Picasa will use /var/opt/picasa/$LOGNAME/picasa.dir).
- by creating a directory /usr/local/data with mode 1777
  (in which case Picasa will use /usr/local/data/$LOGNAME/picasa.dir).
If you make this change, be sure to remove the ~/.picasa directory we're about to create.
See also http://wiki.winehq.org/HomeDirectoryOnNFS."
        fi
    fi
fi

# Test for broken or incorrect WINEPREFIX and fix it up
[ -d "$WINEPREFIX" ] || mkdir -p $WINEPREFIX
[ -d "$WINEPREFIX/drive_c" ] || mkdir -p $WINEPREFIX/drive_c
[ -d "$WINEPREFIX/drive_c/Program Files" ] || mkdir -p "$WINEPREFIX/drive_c/Program Files"
[ -d "$WINEPREFIX/drive_c/windows" ] || mkdir -p $WINEPREFIX/drive_c/windows
[ -d "$WINEPREFIX/drive_c/windows/temp" ] || mkdir -p $WINEPREFIX/drive_c/windows/temp
[ -d "$WINEPREFIX/drive_c/windows/inf.done" ] || mkdir -p $WINEPREFIX/drive_c/windows/inf.done
[ -d "$WINEPREFIX/drive_c/windows/profiles" ] || mkdir -p $WINEPREFIX/drive_c/windows/profiles
[ -d "$WINEPREFIX/drive_c/windows/profiles/All Users/Start Menu/Programs" ] || mkdir -p "$WINEPREFIX/drive_c/windows/profiles/All Users/Start Menu/Programs"
[ -d "$WINEPREFIX/dosdevices" ] || mkdir -p $WINEPREFIX/dosdevices

# Repair/establish symlink dirs
for sdir in \
           "drive_c/Program Files/wine_gecko" \
	   drive_c/windows/command  \
	   drive_c/windows/fonts \
	   drive_c/windows/inf \
	   "drive_c/windows/profiles/All Users/Start Menu/Programs/Picasa2" \
	   drive_c/windows/system \
	   drive_c/windows/system32  \
  ; do 
  # Remove broken links first, in case they moved Picasa somewhere
  if [ -h "$WINEPREFIX/$sdir" ] ; then
    if [ `stat --dereference --format %i "$WINEPREFIX/$sdir"` = `stat --dereference --format %i "$mydir/../wine/$sdir"` ] ; then
    	continue;
    fi
    [ -d "$WINEPREFIX/$sdir" ] || rm -f "$WINEPREFIX/$sdir"
  fi

  # Now link it
  [ -d "$WINEPREFIX/$sdir" ] || ln -s "$mydir/../wine/$sdir" "$WINEPREFIX/$sdir"

done

# Upgrade alert - we're no longer symlinking all of Program Files/Picasa2, just individual dirs
[ -h "$WINEPREFIX/drive_c/Program Files/Picasa2" ] && rm -f "$WINEPREFIX/drive_c/Program Files/Picasa2"  

# Make the Program Files/Picasa2 directory, mostly full of symlinks
[ -d "$WINEPREFIX/drive_c/Program Files/Picasa2" ] || mkdir -p "$WINEPREFIX/drive_c/Program Files/Picasa2"  
(cd "$mydir/../wine/drive_c/Program Files/Picasa2";
    for x in * ; do 
      # Remove broken links first, in case they moved Picasa somewhere
      oldlink="`pwd`/$x"
      newlink="$WINEPREFIX/drive_c/Program Files/Picasa2/$x"
      if [ -h "$newlink" ] ; then
	    if [ `stat --dereference --format %i "$oldlink"` = `stat --dereference --format %i "$newlink"` ] ; then
		continue;
	    fi
        [ -d "$newlink" ] || rm -f "$newlink"
      fi

      # Now link it
      [ -d "$newlink" ] || ln -s "$oldlink" "$newlink"

    done 
)

# The whole point of this exercise is to make temp be writable, make dang sure it's
#	not symlinked (should only be a problem during upgrades from 2.2.2820-3)
[ -h "$WINEPREFIX/drive_c/Program Files/Picasa2/temp" ] && rm -f "$WINEPREFIX/drive_c/Program Files/Picasa2/temp"  


# Repair/establish symlink files
for sfile in generic.ppd ; do 

  # Remove broken links first, in case they moved Picasa somewhere
  if [ -h "$WINEPREFIX/$sfile" ] ; then
    [ -f "$WINEPREFIX/$sfile" ] || rm -f "$WINEPREFIX/$sfile"
  fi

  # Now link it
  [ -f "$WINEPREFIX/$sfile" ] || ln -s "$mydir/../wine/$sfile" "$WINEPREFIX/$sfile"

done

# Make sure the dosdevices are set up
if [ -h "$WINEPREFIX/dosdevices/c:" ] ; then
  [ -d "$WINEPREFIX/dosdevices/c:" ] || rm -f "$WINEPREFIX/dosdevices/c:"
fi
[ -d "$WINEPREFIX/dosdevices/c:" ] || ln -s "$WINEPREFIX/drive_c/" "$WINEPREFIX/dosdevices/c:"

if [ -h "$WINEPREFIX/dosdevices/z:" ] ; then
  [ -d "$WINEPREFIX/dosdevices/z:" ] || rm -f "$WINEPREFIX/dosdevices/z:"
fi
[ -d "$WINEPREFIX/dosdevices/z:" ] || ln -s / "$WINEPREFIX/dosdevices/z:"

if [ -h "$WINEPREFIX/dosdevices/y:" ] ; then
  [ -d "$WINEPREFIX/dosdevices/y:" ] || rm -f "$WINEPREFIX/dosdevices/y:"
fi
if [ -d "$HOME" ] ; then
    [ -d "$WINEPREFIX/dosdevices/y:" ] || ln -s "$HOME" "$WINEPREFIX/dosdevices/y:"
fi

# Work the argument to find the thing to run
if [ $# -gt 0 ] ; then
    if [ "$1" != "wineserver" ] ; then
      if [ -f "$1" -o "$1" = "PicasaMediaDetector" ] ; then
        exe="$1"
        shift
      else
        exe=`find "$WINEPREFIX/drive_c/Program Files/" -follow -iname "$1"`
        if [ ! -f "$exe" ] ; then
          exe=`find "$WINEPREFIX/drive_c/Program Files/" -follow -iname "$1.exe"`
          if [ ! -f "$exe" ] ; then
            exe=`find "$mydir/../wine/lib/" -iname "$1"`
            if [ ! -f "$exe" ] ; then
              exe=`find "$mydir/../wine/lib/" -iname "$1.so"`
              if [ ! -f "$exe" ] ; then
                exe=`find "$mydir/../wine/lib/" -iname "$1.exe.so"`
              fi
            fi
          fi
        fi
        if [ -z "$exe" ] ; then
          exe="$1"
        fi
        shift
      fi
    fi
fi

# Redirect output to /dev/null if we're not debugging
if test "z$WINEDEBUG" = z ; then
    exec >/dev/null 2>&1
fi

# FIXME - Only US English has been well tested to this point,
#         so we unset all LOCALE strings, causing us to default to Posix and hence US English
 for x in `locale 2>/dev/null | sed 's/=.*$//'` ; do 
    unset $x 
done

# Apply the .inf files (reapply if they've changed)
for infcmd in wine.inf/DefaultInstall mozctl.inf/Mozctl picasa.inf/DefaultInstall ; do
    inffile=`echo $infcmd | cut -d '/' -f 1`
    inftype=`echo $infcmd | cut -d '/' -f 2`

    runreg=0
    if [ -f "$WINEPREFIX/drive_c/windows/inf.done/$inffile" ] ; then
        diff -q "$WINEPREFIX/drive_c/windows/inf/$inffile" "$WINEPREFIX/drive_c/windows/inf.done/$inffile"  >/dev/null 2>&1
        if [ $? -ne 0 ] ; then
            runreg=1
        fi
    else
        runreg=1
    fi

    if [ $runreg -gt 0 ] ; then
        "${WINELOADER:-wine}" rundll32.exe setupapi.dll,InstallHinfSection $inftype 128 $inffile
        cp "$WINEPREFIX/drive_c/windows/inf/$inffile" "$WINEPREFIX/drive_c/windows/inf.done/$inffile" 
    fi
done

# Go
if [ "$1" = "wineserver" ] ; then
    shift
    exec "${WINESERVER:-wineserver}" $* 
else
    exec "${WINELOADER:-wine}" "$exe" $* 
fi
