#!/bin/sh
# test we are root
if [ "$USER" != "root" ] ; then
   echo "Run as root please, exiting." 
   exit 1                                                      
fi

# Juanito flags
export CC="gcc -mtune=generic -Os -pipe" 
export CXX="g++ -mtune=generic -Os -pipe"

P=vlc
USER=`cat /etc/sysconfig/tcuser`

# fribidi-dev in libass-dev # libogg-dev, flac-dev and libvorbis-dev inside lame-dev
LIST="compiletc submitqc5 qt-5.x-dev libmad-dev libgcrypt-dev libdvdnav-dev \
faad-dev  libtheora-dev x264-dev libass-dev libvdpau-dev  librsvg-dev libva-dev \
pulseaudio-dev avahi-dev gnutls-dev taglib-dev automake ffmpeg-dev gettext-dev \ 
libbluray-dev  libcddb-dev speex-dev libmpeg2-dev lua-dev "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc  \
http://download.videolan.org/pub/videolan/vlc/2.2.1/vlc-2.2.1.tar.xz                                
" $USER 

# we are root for rest of commands
xz -d $P-2.2.1.tar.xz
tar xvf $P-2.2.1.tar
cd $P-2.2.1
### not used     sed -i 's:libsmbclient.h:samba-4.0/&:' modules/access/smb.c
### not used     find . -name Makefile -type f -exec sed -i 's/-O2//g' {} \;

# configure default=/usr/local
./configure --disable-static  --disable-a52  --disable-smbclient --enable-screen
make -j3                    
mkdir -p /tmp/$P
make install DESTDIR=/tmp/$P

# Strip executables
cd /tmp/$P
find . | xargs file | grep "executable"    | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
cd /tmp
##########
# create dev
##########
mkdir -p $P-dev/usr/local/lib/vlc/plugins

LISTD="access access_output audio_filter \
audio_mixer audio_output  codec control \
demux gui lua meta_engine misc mux \
packetizer services_discovery stream_filter \
stream_out text_renderer vdpau \
video_chroma video_filter video_output
video_splitter  visualization"
for Z in $LISTD
do
SRC=/tmp/vlc/usr/local/lib/vlc/plugins/$Z
mkdir /tmp/vlc-dev/usr/local/lib/vlc/plugins/$Z
DEST=/tmp/vlc-dev/usr/local/lib/vlc/plugins/$Z
cd  $SRC
mv *.la $DEST
cd /tmp
done

mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
mv $P/usr/local/lib/*.la $P-dev/usr/local/lib/
mv $P/usr/local/lib/vlc/*.a $P-dev/usr/local/lib/vlc/
mv $P/usr/local/lib/vlc/*.la $P-dev/usr/local/lib/vlc/

# create locale
##############
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# main
# part one for youtube entry
######
rm -rf $P/usr/local/share/kde4
mkdir $P/usr/local/share/pixmaps
mkdir -p $P/usr/local/lib/vlc/lua/playlist
mkdir -p $P/usr/local/share/vlc/ssl/certs
cp $P/usr/local/share/icons/hicolor/32x32/apps/vlc.png $P/usr/local/share/pixmaps/vlc.png
rm -rf $P/usr/local/share/applications/$P.desktop
ln -s /usr/local/share/ca-certificates/mozilla/*  \
$P/usr/local/share/vlc/ssl/certs/
cp $P/usr/local/share/doc/$P/lua/playlist/youtube.lua \
$P/usr/local/lib/vlc/lua/playlist

# main create simple desktop
##################
echo "[Desktop Entry]
Name=VLC media player
GenericName=Media player
Comment=Read, capture, broadcast your multimedia streams
TryExec=/usr/local/bin/vlc
Exec=/usr/local/bin/vlc 
Icon=vlc
Type=Application
Categories=AudioVideo;Player;Recorder;
Keywords=Player;Capture;DVD;Audio;Video;Server;Broadcast;
X-FullPathIcon=/usr/local/share/pixmaps/vlc.png" \
 > $P/usr/local/share/applications/$P.desktop

# create doc  
##########
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/share/doc $P-doc/usr/local/share/
mv $P/usr/local/share/man $P-doc/usr/local/share/

# TCZ them
#######
LIST2="$P $P-dev $P-doc $P-locale "
for Z in $LIST2
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
	cd /tmp
done

# create depend files
echo "qt-5.x-opengl.tcz
libmad.tcz
libgcrypt.tcz
libdvdnav.tcz
faad.tcz
libass.tcz
librsvg.tcz
alsa.tcz
taglib.tcz
libavdevice.tcz
libbluray.tcz
libmpeg2.tcz
sdl.tcz
lua.tcz
lua-lib.tcz
gnutls.tcz
 " > vlc.tcz.dep

echo "vlc.tcz" > vlc-locale.tcz.dep

# create info files let submitqc5 add size

echo "Title:          vlc.tcz  TESTING
Description:    audio and video media player
Version:        2.2.1
Author:         see doc        
Original-site:  http://videolan.org
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           vlc av video play stream encode youtube
Comments:      Can optionally use pulseaudio.
                         Media player, streamer and encoder.

                         Can stream youtube see TCE news.
 
                	Compiled for Corepure64 6.x
Change-log:     2014/01/17 first version
             		2015/05/3 updated 2.1.1 -> 2.2.1 Juanito
Current:     2015/08/03 add youtube support 
" > vlc.tcz.info

echo "Title:          vlc-dev.tcz
Description:    Dev files for vlc
Version:        2.2.1
Author:         see doc 
Original-site:  http://videolan.org
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           vlc 
Comments:       Development files

		Compiled for Corepure64 6.x
Change-log:     2015/08/03 First version 
Current:        2015/08/03
" > vlc-dev.tcz.info

echo "Title:          vlc-doc.tcz
Description:    Some docs  for vlc
Version:        2.2.1
Author:         see AUTHORS
Original-site:  http://videolan.org
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           vlc 
Comments:       Documents and scripts 

		Compiled for Corepure64 6.x
Change-log:     2015/08/03 First version 
Current:        2015/08/03
" > vlc-doc.tcz.info

echo "Title:          vlc-locale.tcz
Description:    locale files for vlc
Version:        2.2.1
Author:         see doc
Original-site:  http://videolan.org
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           vlc 
Comments:       For non-English users

		Compiled for Corepure64 6.x
Change-log:     2015/08/03 First version 
Current:        2015/08/03
" > vlc-locale.tcz.info

submitqc5