#!/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.6.0
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

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    "  $USER
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 15 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
#########  
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* 
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|g' -i $PREF

# dir already exists
echo 'LGPL v2' > $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 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.6.0
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:       -
                
Change-log:     2020/07/22 v 1.7.0 on 11x
                2023/03/28 v 3.4.7 on 15x html files added
Current:        2024/08/02 v 3.6.0     ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          icewm-locale.tcz
Description:    locales 
Version:        3.6.0
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
                (upgrade loses Slovenia) si
                Consider keeping existing if this affects you.
                
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 
Current:        2024/08/02 v 3.6.0      ' > $P-locale.tcz.info
echo 'icewm.tcz' > $P-locale.tcz.dep

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

echo 'Title:          icewm.tcz
Description:    Stacking Window Manager 
Version:        3.6.0
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. 
                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

                If you see a locale error in your terminal by running 
                $ icewm -V 
                Paste the following into your .ashrc (change lang to suit)
                export LC_ALL="en_US.UTF-8" && export LC_CTYPE="en_US.UTF-8"
                (and I have no mention of G_FILENAME_ENCODING=@locale
                or any other locale setting in .ashrc .bashrc .profile)

New Features
Double-click anywhere in titlebar maximizes App
Double-click edge (when pointer changes to resize) -> maximizes that side
Double-click corners to maximize that corner. 

Double-click to reverse above options.
                
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 
Current:        2024/08/02 v 3.6.0 ' > $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 -> cairo
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2 ->    librsvg
#libcairo.so.2]                        cairo
#libImlib2.so.1]               imlib2
#libXpm.so.4]                 libXpm
#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 -> fontconfig -> cairo
#libXrender.so.1]libXrender -> libXft
#libXcomposite.so.1]                     libXcomposite
#libXdamage.so.1]                     libXdamage
#libXfixes.so.3]                libXfixes
#libXext.so.6] libXext ->         aterm
#libX11.so.6] libX11 -> -> cairo
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

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

submitqc --libs
rm -rf *.zsync

# icewm-list to be deleted from repo
