#### this file is sourced not run
PKGVER=1.18.1
PKGBUILD=1
PKGARCH=x86_64

# source: james
TARBALL=ppsspp-$PKGVER.tar.xz
MD5SUM=2fef8f022c170814aa78f3e9b4a00b94
SRC_URL=https://github.com/hrydgard/ppsspp/releases/download/v$PKGVER/$TARBALL
BUNDLE=

# pre-compiled ffmpeg lib from ppsspp devs
FFMPEG_TARBALL=ppsspp-ffmpeg-lib-x86_64-82049cc.tar.gz
FFMPEG_MD5SUM=24ff744a1fe401b6b26a9500594b51af
FFMPEG_SRC_URL=http://distro.ibiblio.org/fatdog/source/900/$FFMPEG_TARBALL
# actual source: https://github.com/hrydgard/ppsspp-ffmpeg

SLACKREQ='libSDL2 libSDL2-ttf libpng libglew' # and OpenGL (mesa), python3 build-time 
SLACKDESC="ppsspp: ppsspp $PKGVER (PSP Emulator)
ppsspp: 
ppsspp: A fast and portable PSP emulator, written in C++.
ppsspp: 
ppsspp: https://www.ppsspp.org/
ppsspp: https://github.com/hrydgard/ppsspp/
ppsspp: 
ppsspp: 
ppsspp: 
ppsspp: 
ppsspp: 
"
SLACKSUG='libvulkan'

### download
pkg_download() {
	pkg_check $SRC_DIR $FFMPEG_TARBALL $FFMPEG_MD5SUM $FFMPEG_SRC_URL &&
	pkg_check $SRC_DIR $TARBALL        $MD5SUM        $SRC_URL
}

### prepare
pkg_prepare() {
	pkg_download &&
	tar -xf $SRC_DIR/$TARBALL -C ./tmp &&
	
	# this pre-compiled stuff is needed for USE_FFMPEG=ON and USE_SYSTEM_FFMPEG=OFF
	# if we don't have this, we have to enable USE_SYSTEM_FFMPEG=ON, but video is corrupted
	tar -xf $SRC_DIR/$FFMPEG_TARBALL -C ./tmp/ppsspp-$PKGVER/ffmpeg/linux &&

	return 0
}

### default pkg_package
### build
pkg_build() {		
	cd /tmp/ppsspp* &&

	# preserve git version - we don't have git. For git-master only
	#sed -i -e 's/PPSSPP_GIT_VERSION_NO_UPDATE 0/PPSSPP_GIT_VERSION_NO_UPDATE 1/' git-version.cpp &&

	#CXXFLAGS="$CXXFLAGS -fpermissive" && # needed for building with USE_SYSTEM_FFMPEG=ON
	
	mkdir build && cd build &&
	cmake .. \
		-DCMAKE_INSTALL_PREFIX=/usr \
	&&
	# -DUSING_X11_VULKAN=OFF # it is on by default

	make $MAKEFLAGS &&
	make install &&

<< "MANUAL_INSTALL_NOT_USED_ANYMORE" &&
	# manual install
	mkdir -p /opt/PPSSPP-$PKGVER &&
	install -m755 PPSSPPSDL /opt/PPSSPP-$PKGVER &&
	cp -a assets /opt/PPSSPP-$PKGVER &&
	
	# wrapper script
	cat > /usr/bin/PPSSPP << EOF &&
#!/bin/dash
cd /opt/PPSSPP-$PKGVER
exec ./PPSSPPSDL "\$@"
EOF
	chmod +x /usr/bin/PPSSPP &&

	# desktop
	mkdir -p /usr/share/icons /usr/share/applications &&
	install -m644 /tmp/pkg/PPSSPP.desktop /usr/share/applications &&
	ln -s /opt/PPSSPP-$PKGVER/assets/icon_regular_72.png /usr/share/icons/PPSSPP.png &&
MANUAL_INSTALL_NOT_USED_ANYMORE

	# finish
	pkg_build_slackdesc
}
