## Début script windows ##
#!/bin/sh
# variables à changer :
export BASEDIR=$PWD/wincross

COMPILER=vc12
ARCH=amd64
WINEVERSION=1.9.1

URL="http://kervala.net/download/"
URLPREFIX="win32_cross_"
URLSUFFIX=".7z"

download()
{
  PACK=$1

  if [ ! -e $URLPREFIX$PACK$URLSUFFIX ]
  then
    echo "Downloading $PACK... [$URL$URLPREFIX$PACK$URLSUFFIX]"
    wget -q $URL$URLPREFIX$PACK$URLSUFFIX || exit 2
  fi

  echo "Extracting $PACK..."
  7z -y -o$BASEDIR x $URLPREFIX$PACK$URLSUFFIX 1> /dev/null || exit 2

  RET=$?

  if [ -d $BASEDIR/$PACK/bin ]
  then
    chmod +x $BASEDIR/$PACK/bin/*.exe
  fi

  return $RET
}





if [ "$COMPILER" = "vc8" ]
then
  echo "Using VC++ 2005 compilers"
elif [ "$COMPILER" = "vc9" ]
then
  echo "Using VC++ 2008 compilers"
elif [ "$COMPILER" = "vc10" ]
then
  echo "Using VC++ 2010 compilers"
elif [ "$COMPILER" = "vc11" ]
then
  echo "Using VC++ 2012 compilers"
elif [ "$COMPILER" = "vc12" ]
then
  echo "Using VC++ 2013 compilers"
elif [ "$COMPILER" = "vc14" ]
then
  echo "Using VC++ 2015 compilers"
else
  echo "Bad compiler $COMPILER"
  return 1
fi

if [ ! -d "tools" ]
then
  download tools
fi

if [ ! -d $COMPILER ]
then
  download $COMPILER

  ARCHS="amd64 ia64 x86 x86_amd64 x86_ia64"

  for TARCH in $ARCHS
  do
    CDIR=$COMPILER/bin/$TARCH

    if [ -d "$CDIR" ]
    then
      chmod +x $CDIR/*.exe
    fi
  done
fi

if [ ! -d "winsdk" ]
then
  download winsdk
fi

if [ ! -d "dxsdk" ]
then
  download dxsdk
fi

if [ ! -d "$COMPILER/external" ]
then
  download $COMPILER"_external"
fi

export BASEDIRDOS=Z:$(echo "$BASEDIR" | sed 's/\//\\/g')

export TOOLSDIR=$BASEDIR/tools
export TOOLSBINDIR=$TOOLSDIR/bin

export VCDIR=$BASEDIR/$COMPILER
export VCBINDIR=$VCDIR/bin

export WINSDKDIR=$BASEDIR/winsdk
export WINSDKBINDIR=$WINSDKDIR/bin

# wine
if [ ! -z $WINEVERSION ]
then
  export BASEWINEDIR=$BASEDIR/wineversion
  export WINEDIR=$BASEWINEDIR/$WINEVERSION/bin
  if [ ! -d $WINEDIR ]
  then
    echo "Downloading WINE $WINEVERSION... [http://www.playonlinux.com/wine/binaries/linux-amd64/PlayOnLinux-wine-$WINEVERSION-linux-amd64.pol]"
    wget -q http://www.playonlinux.com/wine/binaries/linux-amd64/PlayOnLinux-wine-$WINEVERSION-linux-amd64.pol
    echo "Extracting WINE $WINEVERSION... ($BASEDIR)"
    tar xjf PlayOnLinux-wine-$WINEVERSION-linux-amd64.pol --directory=$BASEDIR
  fi
fi

# remove previous win32 pathes af any
export PATH=$(echo $PATH | sed 's|:'$BASEDIR'/[a-z0-9./_-]*||g')


export PATH=$PATH:$WINEDIR


# add wine to path
export CURRENT_CPU=x86
export Configuration=Release
export TARGET_PLATFORM=WIN7
export APPVER=6.1
export DEBUGMSG=Release
export INCLUDE=$BASEDIRDOS"\\"$COMPILER"\\include;"$BASEDIRDOS"\\winsdk\\include"
export LIB=
export LIBPATH=$BASEDIRDOS"\\"$COMPILER"\\lib"

export PATH=$PATH:$TOOLSBINDIR:$WINSDKBINDIR

if [ $ARCH = "x86" ]
then
  export PATH=$PATH:$VCBINDIR/x86
  export WINEPATH=$BASEDIRDOS"\\"$COMPILER"\\bin\\x86;"
  export TARGET_CPU=x86
  export LIB=$BASEDIRDOS"\\"$COMPILER"\\lib;"$BASEDIRDOS"\\winsdk\\lib;"$BASEDIRDOS"\\dxsdk\\lib"
elif [ $ARCH = "amd64" ]
then
  export PATH=$PATH:$VCBINDIR/x86_amd64
  export WINEPATH=$BASEDIRDOS"\\"$COMPILER"\\bin\\x86_amd64;"
  export TARGET_CPU=x64
  export LIB=$BASEDIRDOS"\\"$COMPILER"\\lib\\amd64;"$BASEDIRDOS"\\winsdk\\lib\\amd64;"$BASEDIRDOS"\\dxsdk\\lib\\amd64"
elif [ $ARCH = "ia64" ]
then
  export PATH=$PATH:$VCBINDIR/x86_ia64
  export WINEPATH=$BASEDIRDOS"\\"$COMPILER"\\bin\\x86_ia64;"
  export TARGET_CPU=IA64
  export LIB=$BASEDIRDOS"\\"$COMPILER"\\lib\\ia64;"$BASEDIRDOS"\\winsdk\\lib\\ia64"
else
  echo "Bad arch $ARCH"
  return 1
fi

export LIBPATH=$LIB
export PATH=$PATH:$VCBINDIR

# PATH environment variable for Wine
export WINEPATH=".;"$WINEPATH$BASEDIRDOS"\\"$COMPILER"\\bin;"$BASEDIRDOS"\\winsdk\\bin;"$BASEDIRDOS"\\bin;"$BASEDIRDOS"\\tools\\bin;"$BASEDIRDOS"\\perl\\perl\\bin"

export WINEPREFIX=$BASEDIR/wine/$WINEVERSION

mkdir -p $WINEPREFIX

# launch WINE for the first time
wine ping

# Copy winemenubuilder.exe to wine to avoid missing X11 warnings
#if [ -f $TOOLSBINDIR/winemenubuilder.exe ] && [ -d $WINEPREFIX/drive_c/windows/system32 ]
#then
#  rm -f $WINEPREFIX/drive_c/windows/system32/winemenubuilder.exe
#  rm -f $WINEPREFIX/drive_c/windows/syswow64/winemenubuilder.exe
#  export WINEDLLOVERRIDES=winemenubuilder.exe=n
#fi

# delete fake manifests for VC++ 2008 runtimes
#rm -rf $WINEPREFIX/drive_c/windows/winsxs/x86_microsoft.vc80*
#rm -rf $WINEPREFIX/drive_c/windows/winsxs/manifests/x86_microsoft.vc80*
#rm -f $WINEPREFIX/drive_c/windows/system32/msvcr80.dll
#rm -rf $WINEPREFIX/drive_c/windows/winsxs/x86_microsoft.vc90*
#rm -rf $WINEPREFIX/drive_c/windows/winsxs/manifests/x86_microsoft.vc90*
#rm -f $WINEPREFIX/drive_c/windows/system32/msvcr90.dll
#rm -f $WINEPREFIX/drive_c/windows/system32/msvcr100.dll
#rm -f $WINEPREFIX/drive_c/windows/system32/msvcp100.dll

# Remove all WINE warnings
export WINEDEBUG=-all

# Setting misc environment variables
export QTDIR=Z:$BASEDIR/$COMPILER/qt
export DXSDK_DIR=Z:$BASEDIR/dxsdk
export VC_DIR=Z:$BASEDIR/$COMPILER
#export MT_OPTIONS="-log /tmp/mt/log -verbose"

echo "TARGET_CPU=$TARGET_CPU"
echo "PATH=$PATH"
echo "WINEPATH=$WINEPATH"
echo "LIB=$LIB"
echo "INCLUDE=$INCLUDE"

export EXTERNAL_MSVC80_PATH=Z:$BASEDIR/vc8/external
export EXTERNAL_MSVC90_PATH=Z:$BASEDIR/vc9/external
export EXTERNAL_MSVC10_PATH=Z:$BASEDIR/vc10/external
export EXTERNAL_MSVC11_PATH=Z:$BASEDIR/vc11/external
export EXTERNAL_MSVC12_PATH=Z:$BASEDIR/vc12/external
export EXTERNAL_MSVC14_PATH=Z:$BASEDIR/vc14/external

if [ "$COMPILER" = "vc8" ]
then
  export EXTERNAL_PATH=$EXTERNAL_MSVC80_PATH
elif [ "$COMPILER" = "vc9" ]
then
  export EXTERNAL_PATH=$EXTERNAL_MSVC90_PATH
elif [ "$COMPILER" = "vc10" ]
then
  export EXTERNAL_PATH=$EXTERNAL_MSVC10_PATH
elif [ "$COMPILER" = "vc11" ]
then
  export EXTERNAL_PATH=$EXTERNAL_MSVC11_PATH
elif [ "$COMPILER" = "vc12" ]
then
  export EXTERNAL_PATH=$EXTERNAL_MSVC12_PATH
  export WINEDLLOVERRIDES="msvcr120.dll=n"
elif [ "$COMPILER" = "vc14" ]
then
  export EXTERNAL_PATH=$EXTERNAL_MSVC14_PATH
fi

## Fin script windows ##