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

# includes other-source (SRC2) of a TC64 TCE which supplies mainly the menu files needed for TC
##########################################################################################
export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe" 
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe \
-fno-exceptions -fno-rtti"

P=jwm
V=2.3.7
SRC=$P-$V
SRC2=squashfs-root/usr/local
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools zsync libtool gettext-dev libX11-dev fribidi-dev cairo-dev libXinerama-dev librsvg-dev libjpeg-turbo-dev libXpm-dev libXrender-dev"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp 
su -c "wget -nc http://joewing.net/projects/$P/releases/$SRC.tar.xz \
tinycorelinux.net/11.x/x86_64/tcz/$P.tcz"  $USER

#unpack and remove TCE first so we have no interference
unsquashfs $P.tcz
rm -rf $P.tcz

xz -d $SRC*xz && tar xvf $SRC.tar
mkdir $P
cd $SRC
./configure --prefix=/usr/local --disable-xmu 
##################################################
# Options
# Confirm:  yes
# Icon:     yes
# Cairo:    yes
# RSVG:     yes
# PNG:      yes
# JPEG:     yes
# XBM:      yes
# XPM:      yes
# XFT:      yes
# XRender:  yes
# FriBidi:  yes
# Shape:    yes
# Xmu:      no
# Xinerama: yes
# Debug:    no
#########################
make -j5 # takes seconds
make install DESTDIR=/tmp/$P
cd /tmp

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

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

# main
#####
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE $P/usr/local/share/doc/$P/
strip --strip-unneeded $P/usr/local/bin/*

# tce.install
############# 
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
echo 'jwm' > /etc/sysconfig/desktop
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TC menu fix as per roberts great work
####################################
mv $SRC2/share/$P/tce $P/usr/local/share/$P
rm -rf $SRC2/bin/$P
cp $SRC2/bin/* $P/usr/local/bin/

# there is a glitch on one line
# XML Parsing Error: not well-formed Location: file:///home/tc/.jwmrc-theme Line Number 40, Column 13:
# but its the line above 
THEME=$P/usr/local/share/jwm/tce
sed 's|<Background>#2e3a67</Background|<Background>#2e3a67</Background>|' -i $THEME/.jwmrc-theme

# TCZ them
###########
LIST2="$P $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
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
        zsyncmake $Z.tcz
done

ls -hal

echo 'Title:          jwm-doc.tcz
Description:    jwm docs 
Version:        2.3.7
Author:         Joe Wingbermuelhle
Original-site:  http://joewing.net/projects/jwm/
Copying-policy: accompanied
Size:           12K
Extension_by:   aus9
Tags:           window manager wm
Comments:       mainly man page
                
Change-log:     2020/09/10 Original 2.3.7 on 12x
Current:        2020/09/10' > $P-doc.tcz.info

echo 'Title:          jwm-locale.tcz
Description:    jwm locales 
Version:        2.3.7
Author:         Joe Wingbermuelhle
Original-site:  http://joewing.net/projects/jwm/
Copying-policy: accompanied
Size:           24K
Extension_by:   aus9
Tags:           window manager wm
Comments:       untested by me
                
Change-log:     2020/09/10 Original 2.3.7 on 12x
Current:        2020/09/10   ' > $P-locale.tcz.info

# double quotes below
echo "Title:          jwm.tcz
Description:    Xorg Stacking Window Manager 
Version:        2.3.7
Author:         Joe Wingbermuelhle
Original-site:  http://joewing.net/projects/jwm/
Copying-policy: accompanied
Size:           100K
Extension_by:   aus9
Tags:           window manager wm
Comments:       Joe's Window Manager
                Window focus changes with mouse over new window,
                unless pcmanfm controls your background.
                See TC64 for pcmanfm features, if interested. Remove wbar from
                boot list if you like pcmanfm in control of bg

                Your active window has bright titlebar.

                
Change-log:     2020/09/10 Original 2.3.7 on 12x
Current:        2020/09/10     " > $P.tcz.info

echo 'jwm.tcz' > $P-locale.tcz.dep
echo 'Xorg.tcz
libXpm.tcz
librsvg.tcz' > $P.tcz.dep


