#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

export CFLAGS="-mtune=generic -Os -pipe"
export CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe "

P=vice
O=$P-others
V=3.3
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc xa libpng-dev pulseaudio-dev lame-dev  libpci-dev \
ffmpeg4-dev  bash yasm-dev libpcap-dev libnet-dev texinfo "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://sourceforge.net/projects/$P-emu/files/releases/$SRC.tar.gz/download " $USER 
tar xvf download
mkdir $P
cd $SRC
./configure --without-oss  --disable-debug --enable-ethernet  \
--enable-native-gtk3ui  --enable-external-ffmpeg
make -j5  
make install DESTDIR=/tmp/$P  
cd /tmp

# NOTE the shared objects stripped but not exes make takes 1.5 minutes

# doc
#####
mkdir -p $P-doc/usr/local/share/doc
mv $P/usr/local/lib64/$P/doc $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/man $P-doc/usr/local/share/
mv $P/usr/local/share/info $P-doc/usr/local/share/
cp $SRC/AUTHORS $P-doc/usr/local/share/doc/$P
cp $SRC/README* $P-doc/usr/local/share/doc/$P
rm -rf $P/usr/local/share
# there was a system doc we moved ........mention in info 

# main
#####
# strip
strip --strip-unneeded $P/usr/local/bin/*

# tce.installed
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
LOOP=/tmp/tcloop/vice/usr/local/share/vice/vicerc
HC=/home/$USER/.config/vice
if [ ! -f $HC/vicerc ] ; then
   mkdir -p $HC
   cp $LOOP $HC/
   chmod 644 $HC/vicerc
   chown -R "$USER":staff $HC
fi    ' > $P/usr/local/tce.installed/$P

chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# template  
#########   
mkdir -p $P/usr/local/share/vice
echo 'ChargenName="/usr/local/lib64/vice/C64/chargen" 
KernalName="/usr/local/lib64/vice/C64/kernal"
BasicName="/usr/local/lib64/vice/C64/basic"
DosName1541="/usr/local/lib64/vice/C64/dos1541"
' > $P/usr/local/share/vice/vicerc


# vice OTHERS.....remember O=$P-others
# vice is too big so strip out lots here
##############################
BINP=/tmp/$P/usr/local/bin
BINO=/tmp/$O/usr/local/bin
VP=/tmp/$P/usr/local/lib64/vice
VO=/tmp/$O/usr/local/lib64/vice
mkdir -p $BINO
mkdir -p $VO
mv $BINP/* $BINO
# move back 2 exes
mv $BINO/x64* $BINP/

cd $VP
LIST="C128 CBM-II PET PLUS4  SCPU64 VIC20"
for Z in $LIST
do
    mv $Z $VO/
done 
cd /tmp

# TCZ them
#######
LIST="$P $P-others $P-doc "
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 'man-db.tcz ' > $P-doc.tcz.dep
echo 'vice.tcz' > $P-others.tcz.dep

echo 'gtk3.tcz
xa.tcz
giflib.tcz
libpcap.tcz
libnet.tcz
readline.tcz
libva.tcz
x264_148.tcz
lame.tcz
hicolor-icon-theme.tcz
pulseaudio.tcz  
libavformat4.tcz 
libswscale4.tcz ' > $P.tcz.dep

echo 'Title:          vice.tcz  TESTING
Description:    Commodore Emulator see below
Version:        3.3
Author:         see doc/AUTHORS  
Original-site:  http://vice-emu.sourceforge.net/
Copying-policy: GPL v2
Size:           3.5M		
Extension_by:   aus9 at gmx dot com
Tags:           emulator commodore c64
Comments:       WARNING downloading ROMs may be illegal

                FIRST set up and test sound works for pulseaudio as per its info
                I used both US kb and logitech gamepad F310 in testing.
                No claims made about any being any good.

                App stripped to c64 emulators, all others in vice-others.tcz

                It is important to read most of the docs in the docs TCE please.
                Any reference to 
                /usr/local/lib64/vice/doc is to be READ as
                /usr/local/share/doc/vice-doc/
                So in GUI you can not open the manual in the help pulldown
                Instead load doc and point web browser to index.html
           
                Suggest you mkdir ROMs with local powers and add roms there. 
                Use GUI to attach ROM by navigating to it.

                Quick guide to using T(ape) or D(isk) image ROMs see
                http://vice-emu.sourceforge.net/vice_5.html

                C64 keymap is /usr/local/lib64/vice/C64/gtk3_sym.vkm
                --- for keyboard users

                HINTS----C key is likely to be left Ctrl
                Run/stop key likely to be Esc 
                Space bar may likely have effects too
                Some games expect controller in number 2 position
                Do you see green light on "applet controller" when "direction" pressed?
                Do you see red light in middle of applet controller when you fire?
                applet controller = tiny representations in GUI at bottom.

                Note that you MAY need to change your vicerc file
                to other pathways...from C64 to (eg) C128 etc
                --if you can see that loading of a non-c64 emulator fails

                Compiled for 10.x
Change-log:     2019/03/03  First version 
Current:        2019/03/03      ' > $P.tcz.info

echo 'Title:          vice-others.tcz  TESTING
Description:    Commodore Emulator see below
Version:        3.3
Author:         see doc/AUTHORS  
Original-site:  http://vice-emu.sourceforge.net/
Copying-policy: GPL v2
Size:           8.1M		
Extension_by:   aus9 at gmx dot com
Tags:           emulator commodore 
Comments:       vice is the main package and has the c64* 
                emulators. This App contains the others but depends 
                on vice for essential files.

                Note that you MAY need to change your vicerc file
                to other pathways...from C64 to (eg) C128 etc
                --if you can see that loading of a non-c64 emulator fails

                Please read vice and its doc

                Compiled for 10.x
Change-log:     2019/03/03  First version 
Current:        2019/03/03      ' > $P-others.tcz.info

echo 'Title:          vice-doc.tcz  
Description:    docs for vice
Version:        3.3
Author:         see doc/AUTHORS  
Original-site:  http://vice-emu.sourceforge.net/
Copying-policy: GPL v2
Size:           5.5M		
Extension_by:   aus9 at gmx dot com
Tags:           emulator commodore 
Comments:       Important readmes and info pages.
                HINT point your web browser to 
                /usr/local/share/doc/vice/index.html

                choose your own web browser please.

                Compiled for 10.x
Change-log:     2019/03/03  First version 
Current:        2019/03/03      ' > $P-doc.tcz.info

submitqc --libs