#!/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.7.2
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 libXinerama-dev libXpm-dev libSM-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/ice-wm/$P/releases/download/$V/$SRC.tar.lz   "  $USER
lzip -d $SRC*lz && tar xvf $SRC.tar
cd $SRC
./configure --prefix=/usr/local --disable-debug --with-theme=NanoBlue --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 20secs
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
rm -rf $P/usr/local/share/$P/keys

cat >> $P/usr/local/share/$P/keys <<'EOF'
# IceWM system key defs located at menu or context menu for Preferences -> key (something)
# Move to workspace to the right of current one= Ctrl + Shift + ->
# Below are user defined 
########################
key "Super+KP_Subtract"     amixer sset Master 5%-
key "Super+KP_Add"          amixer sset Master 5%+

# "Multimedia key" bindings for Xorg
key "XF86AudioLowerVolume"  amixer sset Master 5%-
key "XF86AudioRaiseVolume"  amixer sset Master 5%+
key "XF86AudioMute"         amixer sset Master toggle

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

# elected to leave winoptions as is

# 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/aterm.png aterm
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* cancel-logout* communicator* emacs* fte* gnome* hibernate* java* kde* koules* lock* logout* navigator* \
netscape* reboot* restart* shutdown* shutdown* suspend* xisp* xv* 
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 # reduce icon pathway and below line
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|g' -i $PREF

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

# tce.install
############# 
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

# fix pathway to perl
sed 's|bin|local/bin|' -i $P/usr/local/bin/icewm-menu-xrandr

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

ls -hal

echo 'Title:          icewm-doc.tcz
Description:    man pages and htmls 
Version:        3.7.2
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:           256K
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager man
Comments:       -
                
Change-log:     2020/07/22 v 1.7.0 on 11x
                2023/03/28 v 3.4.7 on 15x html files added
                2024/08/02 v 3.6.0 on 15x
Current:        2025/03/11 v 3.7.2 on 16x     ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          icewm-locale.tcz
Description:    locales 
Version:        3.7.2
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:           648K
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
                2023/03/28 v 3.4.7 on 15x icehelp back in, icon caching ignored, aterm is back in 
                2024/08/02 v 3.6.0 on 15x
Current:        2025/03/11 v 3.7.2 on 16x     ' > $P-locale.tcz.info
echo 'icewm.tcz' > $P-locale.tcz.dep

echo 'Title:          icewm-themes.tcz
Description:    themes 
Version:        3.7.2
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
                Choosing a different theme may destroy volumeicon settings in panel
                so just reset them please     
                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
                2023/03/28 v 3.4.7 on 15x icehelp back in, icon caching ignored, aterm is back in 
                2024/08/02 v 3.6.0 on 15x
Current:        2025/03/11 v 3.7.2 on 16x     ' > $P-themes.tcz.info
echo 'icewm.tcz'   > $P-themes.tcz.dep

echo 'Title:          icewm.tcz
Description:    Stacking Window Manager 
Version:        3.7.2
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.4M
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. 
                For a RH mouse user context=RH mouse aka button 2
                Please read configs under ~/icewm. TCEs named in keyboard 
                shortcuts have no deps set. On my AMD APU I needed Xorg-7.7 
                (2d or 3d) 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
                Alt +z ...Input Alt+z in an empty workspace for maximum safety

                optional TCEs 
                icewm-themes as icewm TCE has only nanoblue 
                icewm-locale & xscreensaver and read their infos please
                menumaker but icewm menu now seems better
                volumeicon  - due to dark panel mouse hover left of net applet RH corner
                ............context menu -> RH mouse for RH mouse users, 
                ............change status icon to white gnome YMMV 
                ............or change theme
                arandr seems better than inbuilt icewm-menu-xrandr     
                
                To allow you to keep wbar, ~/icewm/preferences 
                now reads TaskBarAtTop=1 # 0/1
                
                If you see a locale error in your terminal by running 
                $ icewm -V 
                Paste the following into your .ashrc (change lang to suit)
                export LANG="en_US.UTF-8"  and full reboot.  
                If you use gtk apps read getlocale info please
               
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*   
                2023/03/28 v 3.4.7 on 15x icehelp back in, icon caching ignored, aterm is back in 
                2024/08/02 v 3.6.0 on 15x modifier key was Windows TM logo key
Current:        2025/03/11 v 3.7.2 on 16x, change modifier key to Alt for Apps launchers ' > $P.tcz.info

readelf -d $P/usr/local/bin/icehelp | 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 dejavu fonts
#libgdk_pixbuf-2.0.so.0] gdk-pixbuf2 librsvg
#libcairo.so.2] cairo  (hidden pango) and pango librsvg
#libImlib2.so.1]  imlib2
#libXpm.so.4]   libXpm
#libfribidi.so.0]  fribidi (hidden pango) and pango librsvg
#libXinerama.so.1]  libXinerama
#libXft.so.2] libXft (hidden pango) and pango librsvg
#libXrandr.so.2]libXrandr
#libfontconfig.so.1]fontconfig dejavu fonts
#libfreetype.so.6] freetype imlib2
#libXrender.so.1]libXrender libXrandr
#libXcomposite.so.1]  libXcomposite
#libXdamage.so.1] libXdamage
#libXfixes.so.3] libXfixes  libXdamage
#libXext.so.6] libXext  aterm
#libX11.so.6] libX11 aterm
readelf -d $P/usr/local/bin/icewm | grep 'NEEDED' # extras
# libSM.so.6 libSM  aterm
# libICE.so.6] libICE  aterm
readelf -d $P/usr/local/bin/icesh | grep 'NEEDED'                    # no extras
readelf -d $P/usr/local/bin/icewm-menu-fdo | grep 'NEEDED'  # no extras
readelf -d $P/usr/local/bin/icewm-session | grep 'NEEDED'     # no extras
readelf -d $P/usr/local/bin/icewmbg | grep 'NEEDED'               # no extras
readelf -d $P/usr/local/bin/icewmhint | grep 'NEEDED'              # no extras
# icewm-menu-xrandr is a perl script so perl5 new dep
# dejavu fonts provides a glyph I need

echo 'librsvg.tcz
dejavu-fonts-ttf.tcz  
imlib2.tcz
libXpm.tcz
libXinerama.tcz
libXrandr.tcz
libXcomposite.tcz
libXdamage.tcz
aterm.tcz
perl5.tcz  ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

# submitqc says libXdamage is a duplicate but I can not find it

# icewm-list to be deleted from repo  not yet done
