#!/bin/sh
#
# wine.SlackBuild
# 2024-05-14
#
# Wine lets you run Windows software on other operating systems.
# On x86_64, multilib support is required.
#
# For Direct3D 12, vkd3d is required.
# GECKO_VERSION and MONO_VERSION is defined in dlls/appwiz.cpl/addons.c.

PRGNAM=wine
VERSION=8.0.2

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    arm*) ARCH=arm ;;
       *) ARCH=$(uname -m) ;;
  esac
fi

BUILD=${BUILD:-1}
TAG=${TAG:-scr}
PKGTYPE=${PKGTYPE:-txz}

HOMEPAGE="https://www.winehq.org/"
SOURCE="${PRGNAM}-${VERSION}.tar.xz"
DOWNLOAD="https://dl.winehq.org/wine/source/8.0/${SOURCE}"
MD5SUM="c8ec9a10623f0d9b321061de22b2a1d8"

GECKO="wine-gecko-2.47.3-x86.msi"
GECKO_DOWNLOAD="https://dl.winehq.org/wine/wine-gecko/2.47.3/${GECKO}"
GECKO_MD5SUM="e8bf0350695a633e15877684d5b97baf"

MONO="wine-mono-7.4.0-x86.msi"
MONO_DOWNLOAD="https://dl.winehq.org/wine/wine-mono/7.4.0/${MONO}"
MONO_MD5SUM="a8bbe38855ea74539be25c1b8e3d5990"

REQUIRES=""

MAINTAINER="Gernot WALZL"
EMAIL="gernot.walzl@gmx.at"

DESC="Wine Is Not an Emulator

Wine is an Open Source implementation of the Windows API
on top of X, OpenGL, and Unix.

${HOMEPAGE}"

DOCS="$(pwd)/$0 ANNOUNCE AUTHORS COPYING.LIB LICENSE LICENSE.OLD MAINTAINERS README documentation"

# EOF info

WIN64=${WIN64:-'no'}

# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# exit on error
set -e

# download the source if needed
if [ ! -f $CWD/$SOURCE ]; then
  wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
fi
if [ ! -f $CWD/$GECKO ]; then
  wget -O $CWD/$GECKO $GECKO_DOWNLOAD || exit 1
fi
if [ ! -f $CWD/$MONO ]; then
  wget -O $CWD/$MONO $MONO_DOWNLOAD || exit 1
fi

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi
if [ "$(md5sum $CWD/$GECKO | cut -f 1 -d ' ')" != "$GECKO_MD5SUM" ]; then
  exit 1
fi
if [ "$(md5sum $CWD/$MONO | cut -f 1 -d ' ')" != "$MONO_MD5SUM" ]; then
  exit 1
fi

# extract the application source
cd $TMP || exit 1
tar xvf $CWD/$SOURCE || exit 1

# change to the application source directory
cd $PRGNAM-$VERSION || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# set configure options
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
 elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
 elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
 else
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG/install

WITH_WINE64=""
if [ "$WIN64" = "yes" ]; then
  WITH_WINE64="--with-wine64=../wine64"
  mkdir wine64
  cd wine64

  CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" \
    ../configure \
    --prefix=/usr \
    --libdir=/usr/lib64 \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --mandir=/usr/man \
    --docdir=/usr/doc/$PRGNAM-$VERSION \
    --with-opengl \
    --with-gnutls \
    --enable-win64 \
    --disable-tests

  make depend
  make
  make install DESTDIR=$PKG

  cd ..
fi

mkdir wine32
cd wine32

CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" \
  ../configure \
  --prefix=/usr \
  --libdir=/usr/lib \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --with-opengl \
  --with-gnutls \
  $WITH_WINE64 \
  --disable-tests

make depend
make
make install DESTDIR=$PKG

cd ..

mkdir -p $PKG/usr/share/wine/gecko
cp $CWD/$GECKO $PKG/usr/share/wine/gecko/

mkdir -p $PKG/usr/share/wine/mono
cp $CWD/$MONO $PKG/usr/share/wine/mono/

# add documentation
if [ "$DOCS" != "" ]; then
  mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
  cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
  find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
fi

# compress man pages
if [ -d $PKG/usr/man ]; then
  find $PKG/usr/man -type f -name "*.?" -exec gzip -n9 {} \;
  for i in $(find $PKG/usr/man -type l -name "*.?") ; do
    ln -s $(readlink $i).gz $i.gz
    rm $i
  done
fi

# strip binaries
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# add some description to the package
if [ "$DESC" != "" ]; then
  echo "$DESC" | sed 's/^/'$PRGNAM': /g' > $PKG/install/slack-desc
fi

# append dependency information to the package
if [ "$REQUIRES" != "" ]; then
  for REQ in $REQUIRES; do
    echo "$REQ" >> $PKG/install/slack-required
  done
fi

# doinst.sh
cat > $PKG/install/doinst.sh <<EOF
#!/bin/sh
if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications
fi
EOF

# build the package
cd $PKG
PKGVERSION=$(echo $VERSION | sed 's/-/./g')
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$PKGVERSION-$ARCH-$BUILD$TAG.$PKGTYPE \
  || exit 1

# make everything clean
cd $CWD
rm -rf $PKG
rm -rf $TMP/$PRGNAM-$VERSION
