### this file is sourced not run
PKGVER=7.11
PKGBUILD=1
PKGARCH=i686

# source: james
TARBALL=wine-$PKGVER.tar.xz
MD5SUM=704fcaf8066e4380aaa9bc4a62c4b405
SRC_URL=https://dl.winehq.org/wine/source/7.x/$TARBALL
BUNDLE=

# wine extras
WINE_GECKO_PREFIX=http://dl.winehq.org/wine/wine-gecko/2.47.2
WINE_GECKO=wine-gecko-2.47.2-x86.msi
WINE_GECKO_MD5=e0a26e37397961b2c7f624575590ef66
WINE_GECKO64=wine-gecko-2.47.2-x86_64.msi
WINE_GECKO64_MD5=ff7168b08fa901516fbaa8a66aa3cee6

WINE_MONO_PREFIX=http://dl.winehq.org/wine/wine-mono/7.3.0
WINE_MONO=wine-mono-7.3.0-x86.msi
WINE_MONO_MD5=5801b6522bc1d183968af7d68c423da0

SLACKREQ='mpg123 libtiff libpng libjpeg-turbo fontconfig
gstreamer2 lcms2 freetype2 libgphoto2 sane-backend dbus 
mesa libgnutls libxml2 libxslt openal libtasn1 libpcap
libXfixes libfaudio libvulkan libvkd3d libSDL2
mpg123_32 libtiff32 libpng32 libjpeg-turbo32 fontconfig32
gstreamer2_32 lcms2_32 freetype2_32 libgphoto2_32 sane-backend32 dbus32
mesa32 libgnutls32 libxml2_32 libxslt32 openal32 libtasn1_32 libpcap32
libXfixes32 libfaudio32 libvulkan32 libvkd3d32 libSDL2_32
libusb libusb32'

SLACKDESC="wine: wine $PKGVER (WINE is Not Emulator)
wine: 
wine: Wine enables Linux, Mac, FreeBSD, and Solaris users to run Windows 
wine: applications without a copy of Microsoft Windows. This package 
wine: supports running 32-bit and 64-bit Windows programs.
wine: 
wine: https://wiki.winehq.org/Main_Page
wine: 
wine: 
wine: 
wine: 
"

### wine build config
PKG_NOSTRIP=yes # stripping will kill it (remove some needed info from DLLs)
WINE_ROOT=/opt/wine-$PKGVER
#WINE_ROOT=/usr # if you enable this, make sure you fix the shims

### default pkg_download
### prepare
pkg_prepare() {
	pkg_prepare_default &&
	TARBALL=$WINE_GECKO SRC_URL=$WINE_GECKO_PREFIX/$WINE_GECKO MD5SUM=$WINE_GECKO_MD5 &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM &&
	TARBALL=$WINE_GECKO64 SRC_URL=$WINE_GECKO_PREFIX/$WINE_GECKO64 MD5SUM=$WINE_GECKO64_MD5 &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM &&
	TARBALL=$WINE_MONO SRC_URL=$WINE_MONO_PREFIX/$WINE_MONO MD5SUM=$WINE_MONO_MD5 &&
	pkg_check $SRC_DIR $TARBALL $MD5SUM &&
	wget -O tmp/winetricks https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks &&

	# patches
	cd tmp/wine* &&
	patch -Np1 -i ../pkg/dotnet-hang.patch &&
	sed -i -e '/id=49690/d;/id=49532/d' ../winetricks && # tell winetricks we fixed the bug
	# end patches

	true
}

### package
pkg_package() {
	# add gecko, mono and winetricks
	mkdir -p ./$WINE_ROOT/share/wine/gecko ./$WINE_ROOT/share/wine/mono usr/bin &&
	cp $SRC_DIR/$WINE_GECKO $SRC_DIR/$WINE_GECKO64 ./$WINE_ROOT/share/wine/gecko &&
	cp $SRC_DIR/$WINE_MONO ./$WINE_ROOT/share/wine/mono &&
	if ! [ -e usr/bin/wine ]; then
		ln -sfv $WINE_ROOT/bin/wine usr/bin/wine
	fi &&
	install -t ./usr/bin -m 0755 ./tmp/winetricks &&
	sed -i -e "/^WINETRICKS_VERSION/ aWINESERVER=\${WINESERVER:-${WINE_ROOT}/bin/wineserver}" ./usr/bin/winetricks
}

### build
pkg_build() {
	# use mingw64 to build PE modules if available
	export PATH=$PATH:/opt/mingw-w64-8.0.0_gcc9.3.0/bin

	# build dirs
	mkdir /tmp/build32 /tmp/build64

	# reset these from setup_arch_vars - let wine decide the arch to use
	unset CC CXX
	
	# build 64-bit wine first
	export USE_ARCH=64 PKG_CONFIG_PATH=/usr/lib64/pkgconfig
	cd /tmp/build64 &&
	../wine*/configure --prefix=$WINE_ROOT 	--enable-win64 &&
	make $MAKEFLAGS &&

	# build 32-bit wine
	export USE_ARCH=32 PKG_CONFIG_PATH=/usr/lib/pkgconfig	
	cd /tmp/build32 &&
	../wine*/configure --prefix=$WINE_ROOT --with-wine64=/tmp/build64 &&
	make $MAKEFLAGS &&
	
	# install 32-bit wine first
	make install &&

	# install 64-bit wine
	export USE_ARCH=64 PKG_CONFIG_PATH=/usr/lib64/pkgconfig	
	cd /tmp/build64 &&
	make install &&

	# done
	pkg_build_slackdesc &&

	# install shims
	sed -i -e "s|@WINE_ROOT|$WINE_ROOT|" /tmp/pkg/wine{,64} &&
	install -m755 /tmp/pkg/wine{,64} /usr/bin &&

	# ROX filer integration
	mkdir -p /etc/xdg/rox.sourceforge.net/MIME-types &&
	install -m755 /tmp/pkg/application_x-ms-dos-executable /etc/xdg/rox.sourceforge.net/MIME-types/ &&
	(cd /etc/xdg/rox.sourceforge.net/MIME-types/ &&
	 ln -svf application_x-ms-dos-executable application_x-msi &&
	 ln -svf application_x-ms-dos-executable application_x-ms-shortcut &&
	 ln -svf application_x-ms-dos-executable application_x-ole-storage
	) &&

	# install recommended system commands, symlinking to apploader script
	# cf. https://wiki.winehq.org/Packaging#Wine_Components	
	install -m755 /tmp/build64/tools/wineapploader $WINE_ROOT/bin &&
	(cd /usr/bin &&
	 for p in regedit regsvr32 wineboot winecfg winedbg winefile winepath; do
		ln -svf $WINE_ROOT/bin/wineapploader $p || return 1
	 done
	)

	# install WineApps Launcher
	install -m755 /tmp/pkg/wineapps /usr/bin &&
	install -m644 /tmp/pkg/wineapps.png /usr/share/pixmaps &&
	install -m644 /tmp/pkg/wineapps.desktop /usr/share/applications

}

# vim: ft=sh:

