#!/bin/sh
if [ "$(id -u)" != "0" ]; then
    echo "Error: You need to be root to run this script!" >&2
    exit 1
fi
if ! [ -x /opt/wine-staging/bin/wine ]; then
    R="http://download.opensuse.org/repositories/home:/DarkPlayer:/Pipelight/Debian_7.0 ./"
    K="http://download.opensuse.org/repositories/home:/DarkPlayer:/Pipelight/Debian_7.0/Release.key"
    if grep -q jessie /etc/apt/sources.list; then
        D=jessie
        R="http://repos.fds-team.de/stable/debian $D main"
        K=http://repos.fds-team.de/Release.key
    fi
    if grep -q sid /etc/apt/sources.list; then
        D=sid
        R="http://repos.fds-team.de/stable/debian $D main"
        K=http://repos.fds-team.de/Release.key
    fi
    echo deb $R >/etc/apt/sources.list.d/pipelight.list
    wget -qO- $K|apt-key add -
    apt-get remove --purge --yes $(dpkg -l|awk '/wine/{print $2}'|grep -v libkwine)
    A=
    if [ $(uname -m) = x86_64 ]; then
        A=:i386
        dpkg --add-architecture i386
    fi
    apt-get update && apt-get install --yes wine-staging$A
    apt-get autoremove --purge --yes
fi
printf '#!/bin/sh\n/opt/wine-staging/bin/wine "$@"\n' > /usr/bin/wine
chmod +x /usr/bin/wine
#wget -NP /usr/bin https://winetricks.googlecode.com/svn/trunk/src/winetricks
#wget -NP /usr/bin https://winetricks.googlecode.com/git/src/winetricks
wget -NP /usr/bin https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks
chmod +x /usr/bin/winetricks
if ! grep -q /opt/wine-staging/bin/wineserver /usr/bin/winetricks; then
    patch --no-backup-if-mismatch /usr/bin/winetricks <<'EOT'
@@ -4015,6 +4015,7 @@
             /usr/lib/i386-linux-gnu/wine-development/wineserver \
             /usr/lib/powerpc-linux-gnu/wine-development/wineserver \
             /usr/lib/x86_64-linux-gnu/wine-development/wineserver \
+            /opt/wine-staging/bin/wineserver \
             file-not-found
         do
             if test -x "$x"
EOT
fi

cat <<EOT

Hints:

First use:

WINEARCH=win32 wine winecfg

To enable CSMT (Staging)

And override dwrite to DEACTIVATE it (helps Steam)!

In case you get wrong res after running wine:

xrandr -s 1;xrandr -s 0

EOT
