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

# error: 'dynamic_cast' not permitted with '-fno-rtti'
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe "

P=icewm
V=3.4.7
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc lzip perl5 asciidoc gettext-dev libtool-dev imlib2-dev \
libICE-dev  librsvg-dev libXcomposite-dev libXft-dev libXinerama-dev libXpm-dev libSM-dev   "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

URL=https://i.postimg.cc
cd /tmp 
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/ice-wm/$P/releases/download/$V/$SRC.tar.lz    \
$URL/rFkbjTN0/file-manager.png \
$URL/6Qy1qptS/terminal.png     "  $USER
chown root:root *.png

lzip -d $SRC*lz && tar xvf $SRC.tar
cd $SRC
# configure help claims I can --disable-librsvg but readelf says nope
./configure --prefix=/usr/local --disable-debug --with-theme=NanoBlue \
--disable-gdk-pixbuf --with-x --with-xterm=aterm
                 
######################### 
# Applications: icewm icewm-session icesh icewmhint icewmbg icehelp icewm-menu-fdo
# Image library: imlib2 librsvg-2.0 libxpm
# Audio support: 
# Features: i18n shape xrandr xfreetype xinerama fribidi 
# Paths: PREFIX: /usr/local
# BINDIR: /usr/local/bin
# LOCDIR: /usr/local/share/locale
# LIBDIR: /usr/local/share/icewm
# CFGDIR: /usr/local/etc/icewm
# DOCDIR: /usr/local/share/doc/icewm
# MANDIR: /usr/local/share/man
# XTERMCMD: aterm
###########################
make -j5 # about 25 secs
make install-strip DESTDIR=/tmp/$P
cd /tmp

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

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

# themes -move all then put nanoblue back
########
mkdir -p $P-themes/usr/local/share/icewm/themes/
mv $P/usr/local/share/$P/themes/ $P-themes/usr/local/share/$P
mkdir -p $P/usr/local/share/icewm/themes/
mv $P-themes/usr/local/share/$P/themes/NanoBlue $P/usr/local/share/icewm/themes/

# main
#####
# "improve" keys
KEYS=$P/usr/local/share/$P/keys
rm -rf $KEYS
cat >> $KEYS <<'EOF'
# Super = Windows logo key on US kb, some other keynames include 
# Alt Ctrl -- Note capitals where applicable
# IceWM system key defs located at menu or context menu for Preferences -> key (something)
# Below are user defined 

key "Super+Enter" xterm
key "Super+a" apps
key "Super+c" cpanel
key "Super+e" exittc
key "Super+f" firefox
key "Super+h" xterm -e htop
key "Super+l" leafpad
key "Super+m" mnttool
key "Super+p" pcmanfm
key "Super+q" qmplay2
key "Super+t" mtpaint
key "Super+v" vlc
key "Super+x" xscreensaver-command -lock
key "Super+2" aterm -fn 12x24 
key "Super+z" icewm -r
EOF

# elected to leave winoptions as is

# toolbar prep
##############
mkdir -p $P/usr/local/share/pixmaps
mv *.png $P/usr/local/share/pixmaps

# toolbar
#########  
TOOL=$P/usr/local/share/$P/toolbar
rm -rf $TOOL
cat >> $TOOL  <<'EOF'
# formula is prog "hover-name-to-display" icon-pathway exe-name If no icon use a "-" without quotes
# Below are user defined 
prog "aterm" /usr/local/share/pixmaps/terminal.png aterm
prog "pcmanfm" /usr/local/share/pixmaps/file-manager.png pcmanfm
EOF

# guess gnome users will use TC's gnome in full
rm -rf $P/usr/local/bin/icewm-set-gnomewm

# remove stale icons and others we use exittc
cd $P/usr/local/share/$P/icons
rm -rf bomb* communicator* emacs* fte* help* java* kde* koules* logout* navigator* netscape* reboot* restart* shutdown* \
shutdown* suspend* xisp* 
cd ../taskbar
rm -rf debian* xfree*
cd /tmp

# change preferences some values fail due to pipe in string change to unique @
PREF=$P/usr/local/share/$P/preferences
sed 's|/usr/share/icons||' -i $PREF
sed 's|:/usr/share/pixmaps:||' -i $PREF
sed 's|WorkspaceNames=" 1 ", " 2 ", " 3 ", " 4 "|WorkspaceNames=" 1 ", " 2 ", " 3 ", " 4 ", " 5"| ' -i $PREF
sed 's|# LogoutCommand=""|LogoutCommand="exittc"|' -i $PREF
sed 's@# RebootCommand="test -e /run/systemd/system && systemctl reboot || loginctl reboot"@RebootCommand="exittc"@' -i $PREF
sed 's@# ShutdownCommand="test -e /run/systemd/system && systemctl poweroff || loginctl poweroff"@ShutdownCommand="exittc"@' -i $PREF
sed 's|# TaskBarAtTop=0|TaskBarAtTop=1|' -i $PREF
# change heaps of sizes with next
sed 's|size=12|size=14|' -i $PREF

# dir already exists
echo 'LGPL v2' > $P/usr/local/share/doc/$P/COPYING

# tce.install
############# gtk-update-icon-cache /usr/local/share/icons/Adwaita removed
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
echo "icewm" > /etc/sysconfig/desktop
USER=`cat /etc/sysconfig/tcuser`
DIR=/usr/local/share/icewm
HOME=/home/$USER/.icewm
[ -d $HOME ] || mkdir -p $HOME
[ -f $HOME/prefoverride ] || touch $HOME/prefoverride
[ -f $HOME/preferences ] || cp $DIR/preferences $HOME
[ -f $HOME/toolbar ] || cp $DIR/toolbar $HOME
[ -f $HOME/keys ] || cp $DIR/keys $HOME 
chown -R $USER:staff $HOME 
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 $P-themes"
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
done

ls -hal

echo 'Title:          icewm-doc.tcz
Description:    man pages and htmls 
Version:        3.4.7
Author:         https://github.com/bbidulock/icewm/blob/icewm-1-4-BRANCH/AUTHORS
Original-site:  https://github.com/ice-wm/icewm
Copying-policy: LGPL v2
Size:           248K
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager man
Comments:       no update on 14x
                
Change-log:     2020/07/22 v 1.7.0 on 11x
Current:        2023/03/28 v 3.4.7 on 15x add html files      ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          icewm-locale.tcz
Description:    locales 
Version:        3.4.7
Author:         https://github.com/bbidulock/icewm/blob/icewm-1-4-BRANCH/AUTHORS
Original-site:  https://github.com/ice-wm/icewm
Copying-policy: LGPL v2
Size:           632K
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager wm locale
Comments:       locales untested by me
                
Change-log:     2020/07/22 v 1.7.0 on 11x
                2023/03/20 v 3.3.1 on 14x
Current:        2023/03/28 v 3.4.7 on 15x      ' > $P-locale.tcz.info
echo 'icewm.tcz' > $P-locale.tcz.dep

echo 'Title:          icewm-themes.tcz
Description:    themes 
Version:        3.4.7
Author:         https://github.com/bbidulock/icewm/blob/icewm-1-4-BRANCH/AUTHORS
Original-site:  https://github.com/ice-wm/icewm
Copying-policy: LGPL v2
Size:           288K
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager wm
Comments:       Extra themes to NanoBlue from main TCE
                also see TCE=icewm-Arc-Dark-theme
                
Change-log:     2020/07/22 v 1.7.0 on 11x
                2023/03/20 v 3.3.1 on 14x
Current:        2023/03/28 v 3.4.7 on 15x     ' > $P-themes.tcz.info
echo 'icewm.tcz' > $P-themes.tcz.dep

echo 'Title:          icewm.tcz
Description:    Stacking Window Manager 
Version:        3.4.7
Author:         https://github.com/bbidulock/icewm/blob/icewm-1-4-BRANCH/AUTHORS
Original-site:  https://github.com/ice-wm/icewm
Copying-policy: LGPL v2 
Size:           1.3M
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager wm
Comments:       manual https://tinyurl.com/4tpya6f5
                
                Most preferences can change via menu button or context menu. 
                Please read configs under ~/icewm. TCEs named in keyboard 
                shortcuts have no deps set except lxterminal. I am unable to 
                test on basic X so am using  Xorg-7.7 (2d) as a dep. Be aware 
                that IceWM uses this order: prefoverride over theme over preferences
                
                icewmbg can control background or wallpaper(s) Or use pcmanfm 
                for another wallpaper method  https://tinyurl.com/y3ouvb94

                For all changes to a config file, or any TCE added after boot,
                that has a kb combo set.....needs WM refresh with kb combo of
                logo+z      logo = Super key
                Please input logo+z in an empty workspace for maximum safety

                optional TCEs not already mentioned include: 
                icewm-themes only nanoblue theme in this TCE
                icewm-locale..xscreensaver and read infos please
                menumaker (for menu generation each time you load a new TCE) 
                I prefer it over icewm-menu-fdo              
                
                To allow you to keep wbar, ~/icewm/preferences 
                now reads TaskBarAtTop=1 # 0/1

                On my AMD APU, I needed Xorg-7.7 too....YMMV
                
Change-log:     2020/07/22 v 1.7.0 on 11x
                2020/11/29 Removed /tmp/tcloop/... from path in tce.installed script. (Rich)
                2023/03/20 v 3.2.2 on 14x nanoblue lxterminal delete icehelp*   
Current:        2023/03/28 v 3.4.7 on 15x icehelp back in, icon caching ignored, aterm is back in ' > $P.tcz.info



readelf -d /usr/local/bin/$P | grep 'NEEDED'
###############################ignore TCBS  
#librsvg-2.so.2]librsvg
#libgio-2.0.so.0]libgobject-2.0.so.0 libglib-2.0.so.0}  glib2 -> cairo
#libgdk_pixbuf-2.0.so.0] gdk-pixbuf2
#libcairo.so.2]cairo
#libImlib2.so.1] imlib2
#libXpm.so.4]libXpm
#libSM.so.6]libSM
#libICE.so.6]libICE
#libfribidi.so.0]fribidi
#libXinerama.so.1]libXinerama
#libXft.so.2]libXft
#libXrandr.so.2]libXrandr
#libfontconfig.so.1]fontconfig -> cairo
#libfreetype.so.6] freetype -> imlib2
#libXrender.so.1] libXrender -> libXft 
#libXcomposite.so.1] libXcomposite
#libXdamage.so.1] libXdamage
#libXfixes.so.3] libXfixes
#libXext.so.6]libXext
#libX11.so.6]libX11 -> cairo

echo 'librsvg.tcz
cairo.tcz
gdk-pixbuf2.tcz
imlib2.tcz
libXpm.tcz
libSM.tcz
libICE.tcz
fribidi.tcz
libXinerama.tcz
libXft.tcz
libXrandr.tcz
libXcomposite.tcz
libXdamage.tcz
libXfixes.tcz
libXext.tcz
dejavu-fonts-ttf.tcz
aterm.tcz' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

# icewm-help to be deleted from 15x repo and icewm-list
