#!/bin/sh
if [ "$(id -u)" != "0" ]; then
    echo "Error: You need to be root to run this script!" >&2
    exit 1
fi
D=$(lsb_release -cs)
[ -z "$D" ] && D=wheezy
K=http://repos.wine-staging.com/Release.key
R="http://repos.wine-staging.com/debian $D main"
rm -f /etc/apt/sources.list.d/pipelight.list /etc/apt/sources.list.d/wine-staging.list
if ! grep -q "deb $R" /etc/apt/sources.list; then
    echo deb $R >/etc/apt/sources.list.d/wine-staging.list
fi
if grep -q DarkPlayer: /etc/apt/sources.list; then
    sed -i /DarkPlayer:/d /etc/apt/sources.list
fi
if grep -q fds-team /etc/apt/sources.list; then
    sed -i /fds-team/d /etc/apt/sources.list
fi
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 wine-staging-compat$A
apt-get update && apt-get install --yes wine-staging$A winehq-staging$A
apt-get autoremove --purge --yes
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)

In case you get wrong res after running wine:

xrandr -s 1;xrandr -s 0

EOT
