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

# As reported by Juanito I do not need *la files anymore!
# error: 'dynamic_cast' not permitted with '-fno-rtti'
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe "

P=icewm
V=3.3.1 
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc asciidoc librsvg-dev gettext-dev \
libtool-dev imlib2-dev perl5 lzip "
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 --with-xterm=lxterminal --disable-debug --with-theme=NanoBlue                                 
######################### unable to disable icehelp delete later
# Applications: icewm icewm-session icesh icewmhint icewmbg icehelp icewm-menu-fdo
# Image library: imlib2 libxpm librsvg-2.0
# 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: lxterminal
###########################
make -j5 # about 30 secs
make install-strip DESTDIR=/tmp/$P
cd /tmp

# no doc 
##########
rm -rf $P/usr/local/share/man 
# remove configs that I build 
rm -rf $P/usr/local/share/$P/keys 
rm -rf  $P/usr/local/share/$P/toolbar 

# no help 
#######
rm -rf $P/usr/local/bin/icehelp 
rm -rf $P/usr/local/share/doc/$P/* 

# icewm-list
############
mkdir -p $P-list/usr/local/share/$P-list
echo 'haveged.tcz
librsvg.tcz
lxterminal.tcz
Xorg-7.7.tcz
cairo.tcz
libXpm.tcz
libSM.tcz
libICE.tcz
fribidi.tcz
adwaita-icon-theme.tcz
gtk-update-icon-cache.tcz
dejavu-fonts-ttf.tcz
icewm.tcz' > $P-list/usr/local/share/$P-list/icewm.lst

# list install script
######################
mkdir -p $P-list/usr/local/tce.installed
cat >> $P-list/usr/local/tce.installed/$P-list <<'EOF'
#!/bin/sh
TCEDIR=/etc/sysconfig/tcedir
FILE=/usr/local/share/icewm-list/icewm.lst
[ -f $TCEDIR/icewm.lst ] || cp $FILE $TCEDIR/
EOF
chown -R root:staff $P-list/usr/local/tce.installed
chmod -R 755 $P-list/usr/local/tce.installed

# 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" lxterminal
key "Super+a" apps
key "Super+c" cpanel
key "Super+e" exittc
key "Super+f" firefox
key "Super+h" lxterminal -e htop
key "Super+l" leafpad
key "Super+m" mtpaint
key "Super+p" pcmanfm
key "Super+q" qmplay2
key "Super+v" vlc
key "Super+x" xscreensaver-command -lock
key "Super+2" aterm -fn 12x24 
key "Super+r" 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 "lxterminal" /usr/local/share/icons/Adwaita/24x24/legacy/utilities-terminal.png lxterminal
prog "pcmanfm" /usr/local/share/icons/Adwaita/24x24/legacy/folder-new.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 or we use exittc
cd $P/usr/local/share/icewm/icons
rm -rf bomb* communicator* emacs* fte* help* java* kde* koules* logout* navigator* netscape* reboot* restart* shutdown* \
shutdown* suspend* xisp* xv*
cd ../taskbar
rm -rf debian* xfree*
cd /tmp

# change preferences 
PREF=$P/usr/local/share/icewm/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|# ShutdownCommand="test -e /run/systemd/system && systemctl poweroff"|ShutdownCommand="exittc"|' -i $PREF
sed 's|# RebootCommand="test -e /run/systemd/system && systemctl reboot"|RebootCommand="exittc"|' -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
############# 
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
echo "icewm" > /etc/sysconfig/desktop
gtk-update-icon-cache /usr/local/share/icons/Adwaita

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 755 $P/usr/local/tce.installed

# TCZ them
###########
LIST2="$P $P-list $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-list.tcz
Description:    boot list for your bootloader
Version:        2
Author:         aus9
Original-site:  Tinycore
Copying-policy: opensource
Size:           4.0K
Extension_by:   you
Tags:           window manager wm
Comments:       Do NOT choose OnBoot in Apps please.
                When ready, in Apps choose 
                DownloadOnly (not + load)
                In a terminal run
                $ tce-load -i icewm-list
                $ cat /etc/sysconfig/tcedir/icewm.lst
                Optional you can then elect to add TCEs
                to that list from your current boot list

                If you need firmware, order is important
                see your current boot list or search forum

Change-log:     2020/07/22 Original on 11x
                2020/11/29 Removed /tmp/tcloop/... from path in tce.installed script. (Rich)
Current:        2023/03/20 v2 on 14x
 ' > $P-list.tcz.info

echo 'Title:          icewm-locale.tcz
Description:    locales 
Version:        3.2.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:           588K
Extension_by:   you
Tags:           window manager wm locale
Comments:       locales untested by me
                
Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2023/03/20 v 3.3.1 on 14x
  ' > $P-locale.tcz.info

echo 'Title:          icewm-themes.tcz
Description:    themes 
Version:        3.2.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:   you
Tags:           window manager wm
Comments:       Extra themes to NanoBlue
                
Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2023/03/20 v 3.3.1 on 14x
  ' > $P-themes.tcz.info

echo 'Title:          icewm.tcz
Description:    Stacking Window Manager 
Version:        3.2.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.1M
Extension_by:   you
Tags:           window manager wm
Comments:       manual https://tinyurl.com/4tpya6f5
                
                Do NOT choose OnBoot in Apps please. If you like this 
                WM then download and run icewm-list to get a icewm boot 
                list you can use with your boot loader. Then add TCEs
                This means if you change your mind onboot.lst is unpolluted!
                
                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 forced 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+r      logo = Super key
                Please input logo+r in an empty workspace for maximum safety

                optional TCEs not already mentioned include: icewm-themes
                icewm-locale..xscreensaver and read info please
                menumaker (for menu generation each time you load a new TCE) 
                I prefer it over icewm-menu-fdo              
                
                lxterminal allows apps GUI to display downloads so you do
                not need aterm in your boot list if you wish. If you wish to 
                keep wbar, recommend you edit ~/icewm/preferences 
                remove # so it reads "TaskBarAtTop=1 # 0/1"
                
Change-log:     2020/07/22 Original 1.7.0 on 11x
                2020/11/29 Removed /tmp/tcloop/... from path in tce.installed script. (Rich)
Current:        2023/03/20 v 3.2.2 on 14x nanoblue lxterminal delete icehelp*   ' > $P.tcz.info

echo 'icewm.tcz' > $P-locale.tcz.dep
echo 'icewm.tcz' > $P-themes.tcz.dep

readelf -d /usr/local/bin/$P | grep 'NEEDED'
################################################                                 Xorg-7.7 =xorg
# 0x0000000000000001 (NEEDED) Shared library: [librsvg-2.so.2] librsvg 
# 0x0000000000000001 (NEEDED) Shared library: [libgdk_pixbuf_xlib-2.0.so.0]gdk-pixbuf2 lxterminal
# 0x0000000000000001 (NEEDED) Shared library: [libgio-2.0.so.0]glib2 lxterminal
# 0x0000000000000001 (NEEDED) Shared library: [libgmodule-2.0.so.0]      "
# 0x0000000000000001 (NEEDED) Shared library: [libdl.so.2]   TCB
# 0x0000000000000001 (NEEDED) Shared library: [libresolv.so.2]"
# 0x0000000000000001 (NEEDED) Shared library: [libpng16.so.16]libpng xorg
# 0x0000000000000001 (NEEDED) Shared library: [libz.so.1]TCB
# 0x0000000000000001 (NEEDED) Shared library: [libgobject-2.0.so.0]glib2 lxterminal
# 0x0000000000000001 (NEEDED) Shared library: [libffi.so.7]libffi glib2     " 
# 0x0000000000000001 (NEEDED) Shared library: [libglib-2.0.so.0]  glib2     " 
# 0x0000000000000001 (NEEDED) Shared library: [libpcre.so.1]     pcre glib2 "  
# 0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0] TCB
# 0x0000000000000001 (NEEDED) Shared library: [libcairo.so.2]cairo  
# 0x0000000000000001 (NEEDED) Shared library: [libImlib2.so.1]imlib2 Xlibs Xprogs xorg
# 0x0000000000000001 (NEEDED) Shared library: [libXpm.so.4]libXpm
# 0x0000000000000001 (NEEDED) Shared library: [libSM.so.6]libSM
# 0x0000000000000001 (NEEDED) Shared library: [libICE.so.6]libICE
# 0x0000000000000001 (NEEDED) Shared library: [libfribidi.so.0]fribidi
# 0x0000000000000001 (NEEDED) Shared library: [libXinerama.so.1]libXinerama xorg
# 0x0000000000000001 (NEEDED) Shared library: [libXft.so.2]   libXft         " 
# 0x0000000000000001 (NEEDED) Shared library: [libXrandr.so.2] libXrandr     "
# 0x0000000000000001 (NEEDED) Shared library: [libfontconfig.so.1]fontconfig "
# 0x0000000000000001 (NEEDED) Shared library: [libfreetype.so.6]freetype     "
# 0x0000000000000001 (NEEDED) Shared library: [libXrender.so.1]libXrender    "
# 0x0000000000000001 (NEEDED) Shared library: [libXcomposite.so.1]libXcomposite  "
# 0x0000000000000001 (NEEDED) Shared library: [libXdamage.so.1]libXdamage       "
# 0x0000000000000001 (NEEDED) Shared library: [libXfixes.so.3] libXfixes       "
# 0x0000000000000001 (NEEDED) Shared library: [libXext.so.6] libXext          "
# 0x0000000000000001 (NEEDED) Shared library: [libX11.so.6] libX11            "
# 0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]TCB
# 0x0000000000000001 (NEEDED) Shared library: [libm.so.6]      "
# 0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]  "
# 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]      "
#############################################################
# there may be a change to adwaita and gtk update dep files so I future proof by including both

echo 'haveged.tcz
librsvg.tcz
lxterminal.tcz
Xorg-7.7.tcz
cairo.tcz
libXpm.tcz
libSM.tcz
libICE.tcz
fribidi.tcz
adwaita-icon-theme.tcz
gtk-update-icon-cache.tcz
dejavu-fonts-ttf.tcz' > $P.tcz.dep

submitqc --libs
