#!/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`

LIST="compiletc submitqc cmake qt-5.x-dev ffmpeg7-dev libcdio-paranoia-dev  "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/zaps166/$P1/releases/download/$V/$P1-src-$V.tar.xz  
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

# avoid missing gme on search error...relates to chiptune
sed '277,281d' -i CMakeLists.txt

# to reduce qt6 search and errors
sed '46d;39,43d' -i CMakeLists.txt
sed '33,37d' -i CMakeLists.txt

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 -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
# VkVideo, Build VkVideo acceleration into FFmpeg
# 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
# PulseAudio, Build with PulseAudio module
# PipeWire, Build with PipeWire 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:
# 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
# Chiptune SIDPLAY, Build Chiptune with SIDPLAY support
# 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 -j6 # 2 mins
make -j6 install/strip DESTDIR=/tmp/$P # 2 secs
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*
# reduce complexity of qmplay pausing stopping stuff when members know how to click pause in GUI etc
sed '13,31d' -i $APP/$P.desktop
cd $P/usr/local/bin
ln -s $P1 $P
cd /tmp

# non-std docs were installed
rm -rf $P/usr/local/share/$P

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 . -not -type d | cut -c 2- | sort > /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 pulseaudio pipewire youtube   
Comments:       man page 

Change-log:     2020/07/01 V 20.05.02 11x
                2024/04/27 v 24.04.07 drop libtag support on 15x
Current:        2026/10/03 rebuild against ffmeg7 on 17x ' > $P-doc.tcz.info
echo 'man-db.tcz  ' > $P-doc.tcz.dep

echo 'Title:          qmplay2-locale.tcz
Description:    locales (called lang by package)
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 pulseaudio pipewire youtube  
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
                2024/04/27 v 24.04.07 drop libtag support on 15x
Current:        2026/10/03 rebuild against ffmeg7 on 17x     ' > $P-locale.tcz.info
echo 'qmplay2.tcz ' > $P-locale.tcz.dep

echo 'Title:          qmplay2.tcz
Description:    QT5 alsa,pipewire or pulseaudio media player 
                audio or 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.5M
Extension_by:   aus9 @linuxquestions.org
Tags:           audio video player alsa pulseaudio pipewire youtube    
Comments:       Set up your sound device first, may need more TCEs. I added
                pulseaudio and pipewire support but only tested alsa and pipewire.
                For alsa volume control I use alsamixer from alsa TCE.
                For pipewire I used pavucontrol but please read info files for
                pipewire or pulseaudio please. 

                Can play most formats and has a YouTube browser. For max video choices 
                boot into 3d or a suitable wayland desktop. Load optional vulkan-tools 
                and read the info file if that interests you.
                
                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

                To play audio cd -> Playlist (or Alt + P)..Click "+"  select audiocd 
                DVDs not supported. 
                                                                
                Searching Youtubes will pause this playback 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.
              
                Tested internet radios mp3 and aac streams OK

                Note there are so many settings to turn on/off hardware/software/modules
                ....take your time having a look before requesting help please.
                eg pulseaudio module was turned off first startup for me YMMV

Change-log:     2020/07/01 v 20.05.02 on 11x
                2023/03/21 v 23.02.05 on 14x
                2024/04/27 v 24.04.07 drop libtag support on 15x
Current:        2026/10/03 rebuild against ffmeg7 on 17x    ' > $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] libavformat7
# [libavcodec.so.59] libavcodec7 -> libavformat7
# [libswscale.so.6] libswscale7 
# [libavutil.so.57] libavutil7 -> libswscale7
# [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
# libQt5Network.so.5 
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 -> libavutil7 -> libswscale7
#libEGL.so.1]libEGL
#libvdpau.so.1]libvdpau -> libavutil7 -> libswscale7
#libX11.so.6]libX11
#libXv.so.1]libXv -> submitqc change -> libavdevice7
# libpipewire-0.3.so.0 libpipewire
# libpulse-simple.so.0][libpulse.so.0] libpulseaudio

echo 'dejavu-fonts-ttf.tcz
gtk-update-icon-cache.tcz
glib2-python.tcz
libass.tcz
libavdevice7.tcz
libcdio.tcz
libva22.tcz 
pipewire.tcz
qt-5.x-opengl.tcz  ' > $P.tcz.dep

# pass 1 as submitqc adds "claimed" missing deps added to dep 
# libavdevice7.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.14 added
# 17 update let submit fix redundants

submitqc --libs
rm -rf *.zsync

