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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti "

P=sudoku
V=1.0.5
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc ncursesw-dev "
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://github.com/cinemast/$P/archive/refs/tags/v$V.tar.gz" $USER
tar xvf v$V*gz
cd $SRC
sed 's|usr/bin|usr/local/bin|' -i Makefile # we have another usr/local below
sed 's|-lcurses|-lncursesw|' -i Makefile
sed 's|PREFIX ?|PREFIX |' -i Makefile
sed 's|DESTDIR ?= /|DESTDIR = /tmp/sudoku|' -i Makefile

sed 's|usr|usr/local|g' -i sudoku.c
ln -s /usr/local/include/ncursesw/curses.h /usr/local/include/
make install
cd /tmp

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

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'public domain' > $P/usr/local/share/doc/$P/COPYING
mv $P/usr/local/games $P/usr/local/bin
strip --strip-unneeded $P/usr/local/bin/$P

# 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 'Title:          sudoku-doc.tcz 
Description:    man page
Version:        1.0.5
Author:         Michael Kennett
Original-site:  https://github.com/cinemast/sudoku
Copying-policy: public domain
Size:           8.0K
Extension_by:   aus9 @linuxquestions.org
Tags:           board game puzzle
Comments:       or view html here
https://github.com/cinemast/sudoku/blob/master/sudoku.html

Change-log:     2024/09/11 v 1.0.5 on 15x
Current:        2024/09/11      ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          sudoku.tcz 
Description:    terminal based 9x9 number board game
Version:        1.0.5
Author:         Michael Kennett
Original-site:  https://github.com/cinemast/sudoku
Copying-policy: public domain
Size:           36K
Extension_by:   aus9 @linuxquestions.org
Tags:           board game puzzle
Comments:       $ sudoku # launches random difficulty
                Supported classes are: very easy, easy, medium, 
                hard, and fiendish 
                There is no gap after c eg  $ sudoku -cmedium

Change-log:     2024/09/11 v 1.0.5 on 15x
Current:        2024/09/11      ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED' 
# libncursesw.so.6 ncursesw

echo 'ncursesw.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync
