#!/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"

# REFERENCES 
# https://www.youtube.com/@miyolinux/videos for coordinates mainly
# https://wiki.archlinux.org/title/JWM mainly for $ jwm -p
# mainly errors for ActiveForeground/Background and fixing 

# librsvg-dev deliberately removed to reduce dependency and save almost 7M

P=jwm-full
V=2.4.5
SRC1=jwm-$V
SRC2=squashfs-root
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc imagemagick libjpeg-turbo-dev libpng-dev libXmu-dev \
libXft-dev libXinerama-dev libXpm-dev pango-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/joewing/jwm/releases/download/v2.4.5/jwm-2.4.5.tar.xz \
http://tinycorelinux.net/15.x/x86_64/tcz/jwm-full.tcz "  $USER
unsquashfs jwm-full.tcz
rm -rf jwm-full.tcz
########################

tar xvf jwm-$V*xz
cd $SRC1
./configure --prefix=/usr/local --disable-rsvg
# Confirm:  yes
# Icon:     yes
# Cairo:    no
# RSVG:     no
# PNG:      yes
# JPEG:     yes
# XBM:      yes
# XPM:      yes
# XFT:      yes
# XRender:  yes
# Pango:    yes
# Shape:    yes
# Xmu:      yes
# Xinerama: yes
# Debug:    no
make -j4  # 5s 
make install DESTDIR=/tmp/$P # strip fails
cd /tmp

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

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

# main
#####
# svgs to pngs. The image increases but size of total due to less deps decreases MORE
cd $P/usr/local/share/jwm
convert jwm-blue.svg -resize 48x48 jwm-blue.png 
convert jwm-gray.svg -resize 48x48 jwm-gray.png 
convert jwm-orange.svg -resize 48x48 jwm-orange.png 
convert jwm-red.svg -resize 48x48 jwm-red.png
rm -rf *.svg 
cd /tmp

# Bin
######
strip --strip-unneeded $P/usr/local/bin/jwm
# move all roberts back to bin...no stripping as they are  scripts
mv $SRC2/usr/local/bin/jwm_* $P/usr/local/bin
# even tho unpacked as root they became tc:staff fix
chown root:root $P/usr/local/bin/*
rm -rf $P/usr/local/bin/jwm_ondemand # has issue as per discovered by $ jwm -p

# Minor bin fixes
##########
sed 's|jwm_ondemand||' -i $P/usr/local/bin/jwm_initmenu 
sed 's|aterm|xterm|' -i   $P/usr/local/bin/jwm_menu_common 

# add important roberts files but some need adjusting so some clean create
########################################################
mkdir -p $P/usr/local/share/jwm/tce
cp $SRC2/usr/local/share/jwm/tce/menu* $P/usr/local/share/jwm/tce/
cp $SRC2/usr/local/share/jwm/tce/.jwm* $P/usr/local/share/jwm/tce/

# theme issues and fix
######################
# XML Parsing Error: not well-formed Location: file:///home/tc/.jwmrc-theme Line Number 40, Column 13:
# I am redoing theme as 
# $ jwm -p will reveal errors in independent desktop files....and various jwm files which shows
#JWM: warning: ./.jwmrc-theme[11]: invalid tag in Active: Text
#JWM: warning: ./.jwmrc-theme[12]: invalid tag in Active: Title
#JWM: warning: ./.jwmrc-theme[16]: invalid tag in WindowStyle: Inactive
#JWM: warning: ./.jwmrc-theme[26]: invalid tag in TaskListStyle: ActiveForeground
#JWM: warning: ./.jwmrc-theme[27]: invalid tag in TaskListStyle: ActiveBackground
#JWM: warning: ./.jwmrc-theme[43]: invalid tag in PagerStyle: ActiveForeground
#JWM: warning: ./.jwmrc-theme[44]: invalid tag in PagerStyle: ActiveBackground
#JWM: warning: ./.jwmrc-theme[51]: invalid tag in MenuStyle: ActiveForeground
#JWM: warning: ./.jwmrc-theme[52]: invalid tag in MenuStyle: ActiveBackground 
# https://www.antixforum.com/forums/topic/updating-jwm-theme-scripts/
# eg <Font>Ubuntu-14:style=Regular:antialias=true</Font>
##########################################################################################################################3
rm -rf $P/usr/local/share/jwm/tce/.jwmrc-theme
cat > $P/usr/local/share/jwm/tce/.jwmrc-theme << "EOF"
<JWM>
<WindowStyle>
<Font>sans-10:style=Regular:antialias=false</Font>
<Width>4</Width>
<Height>16</Height>
<Outline>black</Outline>
</WindowStyle>

<TrayStyle>
<Font>sans-12:style=Regular:antialias=false</Font>
<Foreground>black</Foreground>
<Background>#c5c5c5</Background>
</TrayStyle>

<PagerStyle>
<Outline>black</Outline>
<Foreground>#c5c5c5</Foreground>
<Background>#808488</Background>
</PagerStyle>

<MenuStyle>
<Font>sans-12:style=Regular:antialias=false</Font>
<Foreground>black</Foreground>
<Background>#c5c5c5</Background>
<Foreground>white</Foreground>
<Background>#4f5f82</Background>
</MenuStyle>

<PopupStyle>
<Font>sans-12:style=Regular:antialias=false</Font>
<Outline>black</Outline>
<Foreground>white</Foreground>
<Background>#000080</Background>
</PopupStyle>
</JWM>
EOF

# update system config even tho we are not using it
######################
ETC=$P/usr/local/etc/system.jwmrc
sed 's|Audacious|mpg123|' -i $ETC
sed 's|audacious|mpg123|' -i $ETC
sed 's|image|mtpaint|' -i $ETC
sed 's|claws-mail|easymail|' -i $ETC # claws no longer here
sed 's|image|mtpaint|' -i $ETC
sed 's|Gimp|mtpaint|' -i $ETC
sed 's|gimp|mtpaint|' -i $ETC
sed 's|ROX|xfe|' -i $ETC
sed 's|rox|xfe|' -i $ETC
sed '9d' -i $ETC      # no xcalc so delete

# update tray
##############
rm -rf $P/usr/local/share/jwm/tce/.jwmrc-tray
cat > $P/usr/local/share/jwm/tce/.jwmrc-tray << "EOF"
<JWM>
<!-- Additional tray attributes: autohide, width, border, layer, layout -->
<Tray  x="0" y="0" height="30">
<TrayButton icon="jwm-blue">root:1</TrayButton>
<!-- Additional TaskList attribute: maxwidth -->
<TaskList/>
<!-- Additional TrayButton attribute: label -->
<TrayButton label="V" popup="show desktop">showdesktop</TrayButton>
<TrayButton icon="apps" popup="Package Manager">exec:apps</TrayButton>
<TrayButton icon="aterm" popup="terminal">exec:xterm</TrayButton>
<TrayButton icon="cpanel" popup="control panel">exec:cpanel</TrayButton>
<TrayButton icon="editor" popup="text editor">exec:editor</TrayButton>
<TrayButton icon="exittc" popup="exit menu">exec:exittc</TrayButton>
<TrayButton icon="flrun" popup="launcher input box">exec:flrun</TrayButton>
<TrayButton icon="mnttool" popup="mount tool">exec:mnttool</TrayButton>
<TrayButton icon="services" popup="daemons">exec:services</TrayButton>
<TrayButton icon="screenshot" popup="screenshot">exec:screenshot.sh</TrayButton>
<!-- Additional Pager attributes; width, height -->
<Pager/>
<!-- Additional Swallow attribute: height -->
<!-- <Swallow name="watcher" width="0"> watcher </Swallow> -->
<Clock format="%a %d %b %k:%M"></Clock>
<Dock/>
</Tray>
</JWM>
EOF

# update keys
#############
rm -rf $P/usr/local/share/jwm/tce/.jwmrc-keys
cat > $P/usr/local/share/jwm/tce/.jwmrc-keys << "EOF"
<JWM>
<!-- Key bindings -->
<Key key="Up">up</Key>
<Key key="Down">down</Key>
<Key key="Right">right</Key>
<Key key="Left">left</Key>
<Key key="h">left</Key>
<Key key="j">down</Key>
<Key key="k">up</Key>
<Key key="l">right</Key>
<Key key="Return">select</Key>
<Key key="Escape">escape</Key>

<Key mask="SC" key="s">shade</Key>
<Key mask="SC" key="x">close</Key>
<Key mask="SC" key="m">maximize</Key>
<Key mask="SC" key="v">minimize</Key>
<Key mask="SC" key="d">showdesktop</Key>
<Key mask="SC" key="b">resize</Key>
<Key mask="SC" key="z">root:1</Key>
<Key mask="SC" key="n">move</Key>

<Key mask="A" key="Tab">nextstacked</Key>
<Key mask="A" key="F4">close</Key>
<Key mask="A" key="Delete">close</Key>
<Key mask="A" key="#">desktop#</Key>
<Key mask="A" key="F1">root:1</Key>
<Key mask="A" key="F2">window</Key>
<Key mask="A" key="End">exec:exittc</Key>
<Key mask="A" key="Right">rdesktop</Key>
<Key mask="A" key="Left">ldesktop</Key>
<Key mask="A" key="Up">udesktop</Key>
<Key mask="A" key="Down">ddesktop</Key>
<Key mask="A" key="r">restart</Key>
<Key mask="A" key="Return">exec:xterm</Key>
<Key mask="A" key="v">exec:xterm -T "vim" -e vi</Key>
<Key mask="A" key="a">exec:apps</Key>
<Key mask="A" key="e">exec:exittc</Key>
<Key mask="A" key="f">exec:firefox</Key>
<Key mask="A" key="h">exec:xterm -e htop</Key>
<Key mask="A" key="l">exec:leafpad</Key>
<Key mask="A" key="m">exec:mnttool</Key>
<Key mask="A" key="p">exec:pcmanfm</Key>
<Key mask="A" key="2">exec:aterm -fn 12x24</Key>
</JWM>
EOF

# main rc---remove ondemand due to error $ jwm -p + no co-ordinates notification in moving floating app
##########   also add more icon pathways
rm -rf $P/usr/local/share/jwm/tce/.jwmrc
cat > $P/usr/local/share/jwm/tce/.jwmrc << "EOF"
<?xml version="1.0"?>
<JWM>
<!-- The root menu, if this is undefined you will not get a menu. -->
<!-- Additional RootMenu attributes: onroot, labeled, label -->
<RootMenu height="12" onroot="123">
<Include>/usr/local/tce.jwm</Include>
<Include>/tmp/.jwm-sysmenu</Include>
<Separator/>
<Menu label="Exit/Restart">
<Program label="Restart Wbar"> exec wbar.sh</Program>
<Restart label="Restart Window Manager"/>
<Program label="Exit Options"> exec exittc</Program>
</Menu>
</RootMenu>
<Include>./.jwmrc-tray</Include>
<Include>./.jwmrc-theme</Include>
<IconPath>/usr/local/share/pixmaps</IconPath>
<IconPath>/usr/local/share/icons</IconPath>
<IconPath>/usr/local/share/jwm</IconPath>
<IconPath>~/.local/share/icons</IconPath>

<!-- you can copy line below, changing exe, remember to restart and test it $ jwm -p -->
<StartupCommand>pcmanfm</StartupCommand>

<!-- Virtual Desktops -->
<!-- Desktop tags can be contained within Desktops for desktop names. -->
<Desktops width="4">
<!-- Default background. Note that a Background tag can be
     contained within a Desktop tag to give a specific background
    for that desktop. Valid types: solid gradient image tile command
    <Background type="solid">#4f5f82</Background>
    Note: Tiny Core uses a generic setbackground program
       -->
</Desktops>
<!-- Double click speed (in milliseconds) -->
<DoubleClickSpeed>400</DoubleClickSpeed>
<!-- Double click delta (in pixels) -->
<DoubleClickDelta>2</DoubleClickDelta>
<!-- The focus model (sloppy or click) -->
<FocusModel>click</FocusModel>
<!-- The snap mode (none, screen, or border) -->
<SnapMode distance="10">border</SnapMode>
<!-- The move mode (outline or opaque) -->
<MoveMode coordinates="off">outline</MoveMode>
<!-- The resize mode (outline or opaque) -->
<ResizeMode>outline</ResizeMode>
<Include>./.jwmrc-keys</Include>
</JWM>
EOF
################################################################

mkdir -p $P/usr/local/share/doc/$P
cp $SRC1/LICENSE $P/usr/local/share/doc/$P

# 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

# 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 . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo "Title:          jwm-full-doc.tcz
Description:    man page
Version:        2.4.5
Author:         Joe Wingbermuehle
Original-site:  http://joewing.net/projects/jwm
Copying-policy: accompanied
Size:           16K
Extension_by:   aus9 @linuxquestions.org   
Tags:           window manager 
Comments:       see also
                http://joewing.net/projects/jwm/config.html
                http://joewing.net/projects/jwm/guide.html

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

echo "Title:          jwm-full-locale.tcz
Description:    language files
Version:        2.4.5
Author:         Joe Wingbermuehle
Original-site:  http://joewing.net/projects/jwm
Copying-policy: accompanied
Size:           52K
Extension_by:   aus9 @linuxquestions.org   
Tags:           window manager 
Comments:       not tested by me

Change-log:     2024/11/08 v 2.4.5 on 15x
Current:        2024/11/08" > $P-locale.tcz.info
echo 'jwm-full.tcz' > $P-locale.tcz.dep

cat >> $P.tcz.info <<'EOF'
Title:          jwm-full.tcz
Description:    Joe's Window Manager 
Version:        2.4.5
Author:         Joe Wingbermuehle
Original-site:  http://joewing.net/projects/jwm
Copying-policy: accompanied
Documentation:  http://joewing.net/projects/jwm/config.shtml
Size:           128K
Extension_by:   aus9 @linuxquestions.org   
Tags:           window manager 
Comments:       JWM is a floating desktop. Includes some scripts 
                (c) Robert Shingledecker. I am unable to test on libX  

                (UPDATERS) Delete all your $HOME/.jwm* files 
                Too many changes to list reasons/preferences/research in 
                changelog see build script under
                http://tinycorelinux.net/15.x/x86_64/tcz/src/jwm-full
                Main changes:
                (keys) launch keys CA become Alt only
                (keys) aterm becomes xterm to allow lxterminal execute too.
                (theme) helvetica -> sans font
                (theme) popup enabled for mouse hover over icons 
                (tray) understem now V to show desktops
                (tray) height made 20 so I could see the icons on 22" monitor
                (tray) add blue JWM icon for clickable menu  
                (tray) add most fltk clickable icons 
                (rc) stop showing coordinates in moving floating app
                (rc) add more icon pathways to support icon launchers
                (rc) delete support for ondemand due to error for $ jwm -p
                (rc) add auto startup exe for example if not using ~/.X.d
                
                If you add new tray icon launchers, you may need either lxde-icon-theme 
                or humanity-icon-theme as this update no longer has svg support.
                
                Before making changes, backup target file...make changes then run $ jwm -p 
                Fix -> restart jwm from menu -> retest command.  

                BONUS if you use volumeicon it can appear and work as expected in your tray
                but it may disappear if changing theme/settings see info for that TCE
                    
                Optional TCE=jwm-themes

Change-log:     2014/12/01 v 2.2.2 on 5x (maluvia)
Current:        2024/11/08 v 2.4.5 on 15x too many to list here (aus9)
EOF


readelf -d $P/usr/local/bin/jwm | grep 'NEEDED' 
#libX11.so.6]libX11 libXext libXinerama
#libpng16.so.16]libpng freetype fontconfig libXft pango 
#libjpeg.so.62]libjpeg-turbo
#libXft.so.2]libXft pango
#libXrender.so.1]libXrender libXft pango
#[libpangoxft-1.0.so.0]libpangoft2-1.0.so.0]libpango-1.0.so.0]pango
#libgobject-2.0.so.0 libglib-2.0.so.0 glib2 harfbuzz freetype fontconfig libXft pango
#libharfbuzz.so.0]harfbuzz freetype fontconfig libXft pango
#libfontconfig.so.1]fontconfig libXft pango
#libfreetype.so.6]freetype fontconfig libXft pango
#libXpm.so.4]libXpm
#libXext.so.6]libXext libXinerama
#libXmu.so.6]libXmu
#libXinerama.so.1]libXinerama

echo 'libXinerama.tcz
pango.tcz
libjpeg-turbo.tcz
libXpm.tcz
libXmu.tcz' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

