#!/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=xa
V=2.3.9
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc  "
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://www.floodgap.com/retrotech/$P/dists/$SRC.tar.gz " $USER 
tar xvf $SRC*gz
mkdir $P
cd $SRC
# makefile already claims DESTDIR= /usr/local  but make fails to use it so changes
sed 's|DESTDIR|PREFIX|g' -i Makefile
make -j5 # seconds
make install  # fails DESTDIR=/tmp/$P
cd /tmp

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
cp $SRC/COPYING $P-doc/usr/local/share/doc/$P
chmod 644 $P-doc/usr/local/share/doc/$P/*
mv /usr/local/share/man $P-doc/usr/local/share

# main
#####
mkdir -p $P/usr/local/bin
LIST=" xa reloc65 ldo65 file65 printcbm uncpk"
for Z in $LIST
do
	USR=/usr/local/bin
	BIN=$P$USR
	mv $USR/$Z $BIN/
done
cd $P/usr/local/bin
strip --strip-unneeded *
cd /tmp

# TCZ them
#######
LIST="$P $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 'Title:          xa.tcz  
Description:    open-source 6502 cross assembler
Version:        2.3.9
Author:         André Fachat, Jolse Maginnis, David Weinehall and Cameron Kaiser  
Original-site:  https://www.floodgap.com/retrotech/xa
Copying-policy: GPL v2
Size:           60K	
Extension_by:   aus9 at gmx dot com
Tags:           xa xa65
Comments:       xa is a high-speed, two-pass portable cross-assembler
                           Used to compile vice 

                Compiled for 10.x
Change-log:     2018/02/27  First version 
Current:        2018/02/27      ' > $P.tcz.info

echo 'Title:          xa-doc.tcz  
Description:    mainly man pages
Version:        2.3.9
Author:         André Fachat, Jolse Maginnis, David Weinehall and Cameron Kaiser  
Original-site:  https://www.floodgap.com/retrotech/xa
Copying-policy: GPL v2
Size:           24K	
Extension_by:   aus9 at gmx dot com
Tags:           xa xa65
Comments:       man pages 

                Compiled for 10.x
Change-log:     2018/02/27  First version 
Current:        2018/02/27      ' > $P-doc.tcz.info

submitqc --libs