#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   exit 1
fi

# evaded make error: exception handling disabled, use '-fexceptions' to enable
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe "

P1=QMPlay2
P=qmplay2
V=24.04.07
SRC=$P1-src-$V
USER=`cat /etc/sysconfig/tcuser`

# remove taglib-dev for now
LIST="compiletc submitqc cmake qt-5.x-dev ffmpeg5-dev  "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

# not needed anymore 
# cd /usr/local/lib/pkgconfig/
# rm -rf libpulse.pc libpulse-simple.pc libpulse-mainloop-glib.pc

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/zaps166/$P1/releases/download/$V/$P1-src-$V.tar.xz  "  $USER
tar xvf $P1*.xz
cd $SRC

# to evade another make error....found insert newline sed command off the net  
sed '/#include "sndfile.hpp"/a\
#include <limits>\ ' -i src/modules/Modplug/libmodplug/load_j2b.cpp

mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_INSTALL_LIBDIR="/usr/local/lib" \
-DCMAKE_BUILD_TYPE=Release -DUSE_UPDATES=OFF -DUSE_VULKAN=ON -DUSE_PULSEAUDIO=OFF \
-DLANGUAGES=All -DUSE_TAGLIB=OFF 

# Enabled features:
# OpenGL, Build with OpenGL support
# Vulkan, Build with Vulkan support
# Freedesktop notifications, Use Freedesktop notifications
# Use D-Bus power management, Use D-Bus power management
# libass, Build with libass support
# Inputs, Build with Inputs module
# Modplug, Build with Modplug module
# Extensions, Build with Extensions module
# MediaBrowser, Build with MediaBrowser support
# LastFM, Build with LastFM support
# Lyrics, Build with lyrics support..tested
# Radio, Build with Radio Browser support
# YouTube, Build with YouTube support
# MPRIS2, Build Extensions with MPRIS2 support
# Visualizations, Build with Visualizations module
# AudioFilters, Build with AudioFilters module
# VideoFilters, Build with VideoFilters module
# VAAPI, Build VAAPI acceleration into FFmpeg 
# VDPAU, Build VDPAU acceleration into FFmpeg 
# libavdevice, Build FFmpeg with libavdevice suport
# AudioCD, Build with AudioCD module
# ALSA, Build with ALSA module
# XVideo, Build with XVideo module
# CUVID, Build with CUVID module
# Notifications, Build additional notifications module..no need for this most use wbar

# ------Disabled features:
# Qt6 (required version >= 6.1), Build with Qt6
# PCH, Use precompiled headers
# Updates, Build with software updates
# GLSLC, Compile Vulkan shaders
# RubberBand, Build with RubberBand support
# PortAudio, Build with PortAudio module
# TagLib, Build with tags editor
# VkVideo, Build VkVideo acceleration into FFmpeg 
# .... USE_FFMPEG_VKVIDEO enabled if Vulkan is enabled and FFmpeg version is >= 6.1. 
# Chiptune GME, Build Chiptune with GME support
# Chiptune SIDPLAY, Build Chiptune with SIDPLAY support
# PulseAudio, Build with PulseAudio module
# PipeWire, Build with PipeWire module
# FindHwaccelDrivers, Find drivers path for hwaccel
# Git version, Append Git HEAD to QMPlay2 version
# Link Time Optimization, Enable link time optimization for release builds
# Address Sanitizer, Use Address Sanitizer
# Undefined Behavior Sanitizer, Use Undefined Behavior Sanitizer
# SolidActions, Install Solid actions
#############################################################################
make -j5 # about 4 minutes
make -j4 install/strip DESTDIR=/tmp/$P
cd /tmp

# no dev 
########  
rm -rf $P/usr/local/include

# doc
#####
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/share/man $P-doc/usr/local/share/
cd $P-doc/usr/local/share/man/man1
mv $P1* $P.1.gz
cd /tmp

# locale
######## its not the normal format
mkdir -p $P-locale/usr/local/share/$P
mv $P/usr/local/share/$P/lang $P-locale/usr/local/share/$P/

# main
#######
mkdir -p $P/usr/local/share/pixmaps
cp $P/usr/local/share/icons/hicolor/48x48/apps/QMPlay2.png $P/usr/local/share/pixmaps/
APP=$P/usr/local/share/applications
mv $APP/$P1.desktop $APP/$P.desktop
echo 'X-FullPathIcon=/usr/local/share/pixmaps/QMPlay2.png'  >> $APP/$P.desktop
rm -rf $P/usr/local/share/applications/$P1*

cd $P/usr/local/bin
ln -s $P1 $P

# non-std docs were installed
cd /tmp/$P/usr/local/share/qmplay2/
rm -rf AUTHORS ChangeLog LICENSE README.md
cd /tmp

mkdir -p $P/usr/local/share/doc/$P
echo 'LGPL v3' > $P/usr/local/share/doc/$P/LICENSE

# install script
################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
update-mime-database /usr/local/share/mime > /dev/null
gtk-update-icon-cache -q -t -f /usr/local/share/icons/hicolor
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TCZ them
###########
LIST="$P $P-doc $P-locale"
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
        cd /tmp
done

ls -hal

echo 'Title:          qmplay2-doc.tcz
Description:    man page 
Version:        24.04.07
Author:         see AUTHORS
Original-site:  https://github.com/zaps166/QMPlay2
Copying-policy: LGPL v3                
Size:           4.0K
Extension_by:   aus9 @linuxquestions.org
Tags:           audio video player alsa   
Comments:       man page 

Change-log:     2020/07/01 V 20.05.02 11x
Current:        2024/04/27 V 24.04.07 drop taglib support on 15x ' > $P-doc.tcz.info
echo 'man-db.tcz                                                 ' > $P-doc.tcz.dep

echo 'Title:          qmplay2-locale.tcz
Description:    locales 
Version:        24.04.07
Author:         https://github.com/zaps166/QMPlay2/blob/master/AUTHORS
Original-site:  https://github.com/zaps166/QMPlay2
Copying-policy: LGPL v3
Size:           436K
Extension_by:   aus9 @linuxquestions.org
Tags:           audio video player alsa locale  
Comments:       locales not standard pathway, untested

Change-log:     2020/07/01 v 20.05.02 on 11x
                2023/03/21 v 23.02.05 on 14x
Current:        2024/04/27 v 24.04.07 on 15x    ' > $P-locale.tcz.info
echo 'qmplay2.tcz ' > $P-locale.tcz.dep

echo 'Title:          qmplay2.tcz
Description:    QT5 ALSA audio & video player for X or wayland 
Version:        24.04.07 
Author:         https://github.com/zaps166/QMPlay2/blob/master/AUTHORS
Original-site:  https://github.com/zaps166/QMPlay2
Copying-policy: LGPL v3
Size:           2.4M
Extension_by:   aus9 @linuxquestions.org
Tags:           audio video player alsa      
Comments:       Can play all formats supported by FFmpeg, libmodplug, Audio CD, raw files, 
                Rayman 2 music and chiptunes. It contains YouTube browser.

                ALSA (with) X, Xorg 2d/3d or wayland desktops needs to be setup first.
                Dep file does not include above TCEs. For max video choices boot into 
                3d or a suitable wayland desktop. Load optional vulkan-tools and test 
                vkcube-wayland or vkcube is spinning on wayland/Xorg 3d respectively
                QMPlay2 supports spherical view on OpenGL and Vulkan video outputs. 
                Search in youtube TAB for 360 degree or sperical
                Pressing "Ctrl+3" or Playback->Video filters->Spherical view
                -> mouse pointer changes to either a "grabbable hand"  or 4 arrows etc

                Audio users can stay on libX if they prefer.
                                                                
                Searching Youtubes will pause this TCE to download its own yt-dlp to
                ~/.config/QMPlay2 where other files can be found, including log.
                While playing a youtube, you can click context menu, RH click for RH 
                mouse user and choose to download your track. Note that pressing the 
                normal stop button stops playing a track. To stop downloading you need
                to click into the downloader TAB and click the "icon" to the right of 
                download at the top.

                To play audio cd -> Playlist (or Alt + P)
                Click + -> select audiocd
               
                TCE supports hardware video decoding via Settings->Playback settings
                Tested internet radios mp3 and aac streams OK

Change-log:     2020/07/01 v 20.05.02 on 11x
                2023/03/21 v 23.02.05 on 14x
Current:        2024/04/27 v 24.04.07 del libtag support on 15x    ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P1 | grep 'NEEDED'
# [libqmplay2.so] this TCE
# [libQt5Svg.so.5][libQt5Widgets.so.5][libQt5Gui.so.5][libQt5Core.so.5] qt-5.x-base
# [libavformat.so.59] libavformat5
# [libavcodec.so.59] libavcodec5 -> libavformat5
# [libswscale.so.6] libswscale5 
# [libavutil.so.57] libavutil5 -> libswscale5
# [libswresample.so.4] libswresample5 -> libavcodec5 -> libavformat5
# [libQt5DBus.so.5]qt-5.x-dbus
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED'
#libQt5Qml.so.5]libQt5Network.so.5] qt-5.x-base
#libass.so.5] libass
readelf -d $P/usr/local/lib/$P/modules/*.so | grep 'NEEDED'
#libasound.so.2]libasound -> qt-5.x-base
#libcdio.so.18]libcdio
#libcddb.so.2]libcddb -> libcdio
#libva-drm.so.2]libva.so.2]libva2 -> libavutil5 -> libswscale5
#libEGL.so.1]libEGL
#libvdpau.so.1]libvdpau -> libavutil5 -> libswscale5
#libX11.so.6]libX11
#libXv.so.1]libXv -> submitqc change -> libavdevice5

echo 'qt-5.x-base.tcz
libavformat5.tcz
libswscale5.tcz
qt-5.x-dbus.tcz
libass.tcz
libcdio.tcz
libEGL.tcz
libX11.tcz
libXv.tcz
dejavu-fonts-ttf.tcz
python3.9.tcz
libavdevice5.tcz
nettle39.tcz
shared-mime-info.tcz
gtk-update-icon-cache.tcz
qt-5.x-opengl.tcz   ' > $P.tcz.dep

# pass 1 as submitqc adds "claimed" missing deps added to dep 
# libavdevice5.tcz & nettle39.tcz -> this changes dep list too
# pass 2 on launching qmplay2 -> error qt.qpa.plugin: Could not find the Qt platform plugin "xcb" in ""
# solution load qt-5.x-opengl
# pass 2 on launching qmplay2 and searching youtube....needs a python3* TCE loaded  -> python3.9 added

submitqc --libs
rm -rf *.zsync

