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

test on xorg then sway

# 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=23.02.05
SRC=$P1-src-$V
USER=`cat /etc/sysconfig/tcuser`

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

# dirty disable pulseaudio
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
xz -d *xz && tar xvf *tar
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" -DUSE_UPDATES=off 
#################################################3
# Enabled features:
# OpenGL, Build with OpenGL support..tested
# Vulkan, Build with Vulkan support..untested...post tested 
# Freedesktop notifications, Use Freedesktop notifications
# D-Bus suspend, Use D-Bus suspend
# 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
# TagLib, Build with tags editor
# VAAPI, Build VAAPI acceleration into FFmpeg..untested
# VDPAU, Build VDPAU acceleration into FFmpeg..untested
# libavdevice, Build FFmpeg with libavdevice suport
# AudioCD, Build with AudioCD module....tested
# ALSA, Build with ALSA module..tested
# 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
# D-Bus suspend, Use D-Bus suspend
# RubberBand, Build with RubberBand support
# PortAudio, Build with PortAudio module
# 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 -j5 install/strip DESTDIR=/tmp/$P
cd /tmp

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

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

# no doc
#####
rm -rf $P/usr/local/share/man 
rm -rf $P/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
cd $P/usr/local/bin
ln -s $P1 qmplay2
cd /tmp

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

# TCZ them
###########
LIST="$P $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-locale.tcz
Description:    locales 
Version:        23.02.05
Author:         https://github.com/zaps166/QMPlay2/blob/master/AUTHORS
Original-site:  https://github.com/zaps166/QMPlay2
Copying-policy: LGPL v3
Size:           432K
Extension_by:   aus9
Tags:           audio video player alsa locale  
Comments:       locales not standard pathway, untested

Change-log:     2020/07/01 Original 20.05.02 11x
Current:        2023/03/21 v 23.02.05 on 14x    ' > $P-locale.tcz.info

echo 'Title:          qmplay2.tcz
Description:    QT5 alsa media player 
Version:        23.02.05 
Author:         https://github.com/zaps166/QMPlay2/blob/master/AUTHORS
Original-site:  https://github.com/zaps166/QMPlay2
Copying-policy: LGPL v3
Size:           2.2M
Extension_by:   aus9
Tags:           audio video player alsa      
Comments:       Pulseaudio/pipewire disabled by compile. ALSA, Xorg 
                or Xorg 3d or wayland desktop needs to be setup first.

                dep file does not have Xorg* or wayland* set.
                alsa users may need more dependencies.
                                                
                Youtube needs python downloaded + installed.
                TCE downloads and updates youtube-dl to 
                ~/.qmplay2 where other files can be found, 
                including log.

                dejavu-fonts-ttf.tcz provides glyph 0x266a.
                To play audio cd -> Playlist (or Alt + P)
                Click + -> select audiocd

                Vulkan users please read vulkan-tools.
                This TCE can search youtube or internet radio.

                youtube will need python3.9 downloaded and loaded please
                suggest internet radios -> select country first

Change-log:     2020/07/01 Original 20.05.02 11x
Current:        2023/03/21 v 23.02.05 on 14x   ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
#############################################
# [libqmplay2.so] this TCE
# [libtag.so.1]  taglib
# [libQt5Svg.so.5] qt-5.x-base
# [libQt5Widgets.so.5] "
# [libQt5Gui.so.5]     "
# [libavformat.so.58]  libavformat4 libavfilter4
# [libavcodec.so.58] libavcodec4       "
# [libswscale.so.5]  libswscale4       "
# [libavutil.so.56] libavutil4 libswresample4 libavfilter4
# [libswresample.so.3] libswresample4
# [libGL.so.1] libGL qt base
# [libQt5Core.so.5] qt-5.x-base
# [libstdc++.so.6] TCB
# [libm.so.6]      "
# [libgcc_s.so.1]  "
# [libc.so.6]      "
echo 'qmplay2.tcz' > $P-locale.tcz.dep

echo 'taglib.tcz
qt-5.x-base.tcz
qt-5.x-dbus.tcz
libavfilter4.tcz
alsa.tcz
dejavu-fonts-ttf.tcz' > $P.tcz.dep

echo 'qmplay2.tcz ' > $P-locale.tcz.dep

submitqc --libs
