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

######################
# needs libglib-2.0.la
######################
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
export LDFLAGS="-Wl,-O1"

P=icewm
V=1.7.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc asciidoc librsvg-dev  \ 
gettext-dev libtool libtool-dev imlib2-dev lzip \
adwaita-icon-theme perl5"
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
mkdir $P
cd $SRC
./configure --prefix=/usr/local --with-xterm=sakura \
--with-theme=CrystalBlue --disable-debug
##################################################
# Applications: icewm icewm-session icesh icewmhint icewmbg icehelp icewm-menu-fdo
# Image library: gdk-pixbuf-xlib-2.0 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: sakura
#########################
make -j5 # about 15 secs
make install-strip DESTDIR=/tmp/$P
cd /tmp

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

# icewm-list
############
mkdir -p $P-list/usr/local/share/$P-list
echo 'haveged.tcz
Xorg-7.7.tcz
icewm.tcz
sakura.tcz
adwaita-icon-theme.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
LOOP=/tmp/tcloop/icewm-list/usr/local/share/icewm-list/icewm.lst
[ -f $TCEDIR/icewm.lst ] || cp $LOOP $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 C-Blue + 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/CrystalBlue $P/usr/local/share/icewm/themes/
mv $P-themes/usr/local/share/$P/themes/NanoBlue $P/usr/local/share/icewm/themes/

# doc 
##########
mkdir -p $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/man $P-doc/usr/local/share/
cp $SRC/AUTHORS $P-doc/usr/local/share/doc/$P/
cp $SRC/COPYING $P-doc/usr/local/share/doc/$P/
# move configs I intend to change as below
mv $P/usr/local/share/$P/keys $P-doc/usr/local/share/doc/$P/keys-original
mv $P/usr/local/share/$P/toolbar $P-doc/usr/local/share/doc/$P/toolbar-original

# main
#####
# "improve" keys
cat >> $P/usr/local/share/$P/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" sakura -m
key "Super+a"     aterm
key "Super+d"     dmenu_run
key "Super+f"     firefox
key "Super+g"     geany
key "Super+m"     amixer sset Master toggle
key "Super+p"     pcmanfm
key "Super+q"     qmplay2
key "Super+r"     icewm -r
key "Super+v"     vlc
key "Super+x"     xscreensaver-command -lock
EOF


# "improve" toolbar
##############  
cat >> $P/usr/local/share/$P/toolbar <<'EOF'
# formula is "hover-name-to-display" icon-pathway exe-name If no icon use a "-" without quotes
# Below are user defined 
prog "Terminal" /usr/local/share/icons/Adwaita/24x24/legacy/utilities-terminal.png sakura
prog "File Manager" /usr/local/share/icons/Adwaita/24x24/legacy/folder-new.png pcmanfm
prog "Text Editor" /usr/local/share/icons/Adwaita/24x24/legacy/accessories-text-editor.png geany
EOF

# audio not supported so remove components
rm -rf $P/usr/local/bin/icesound

# pretty sure 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* kde* koules* logout* navigator* netscape* reboot* shutdown* xisp* xload* xv*
cd ../taskbar
rm -rf debian.xpm xfree*
cd /tmp

# fix desktops
APP=$P/usr/local/share/xsessions
sed 's|bin|local/bin|' -i $APP/*

# fix icon search pathway only hicolor was correct 
ICONFILE=$P/usr/local/share/icewm/preferences
sed 's|/usr/share/icons/oxygen/base:/usr/share/icons/Adwaita:/usr/share/icons/hicolor:/usr/share/icons:/usr/share/pixmaps|/usr/local/share/icons/Adwaita:/usr/local/share/icons:/usr/local/share/pixmaps|' -i $ICONFILE

# dir already exists
echo 'see doc TCE for COPYING' > $P/usr/local/share/doc/$P/COPYING

# tce.install
############# 
# menu, programs and winoptions not copied over 
# spent no time investigating their effectiveness
 
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`
LOOP=/tmp/tcloop/icewm/usr/local/share/icewm
HOME=/home/$USER/.icewm
[ -d $HOME ] || mkdir -p $HOME
[ -f $HOME/prefoverride ] || touch $HOME/prefoverride
[ -f $HOME/preferences ] || cp $LOOP/preferences $HOME
[ -f $HOME/toolbar ] || cp $LOOP/toolbar $HOME
[ -f $HOME/keys ] || cp $LOOP/keys $HOME 
chown $USER:staff -R $HOME && chmod 644 $HOME/*
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# TCZ them
###########
LIST2="$P $P-doc $P-help $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-doc.tcz
Description:    docs
Version:        1.7.0
Author:         see AUTHORS
Original-site:  https://github.com/bbidulock/icewm
Copying-policy: LGPL v2
Size:           132K
Extension_by:   aus9
Tags:           window manager wm
Comments:       Contains raw config files that I have
                editted in main TCE
                Lots of man pages too

Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2020/07/22
 ' > $P-doc.tcz.info
 
echo 'Title:          icewm-help.tcz
Description:    needed if you click on help
Version:        1.7.0
Author:         see AUTHORS
Original-site:  https://github.com/bbidulock/icewm
Copying-policy: LGPL v2
Size:           116K
Extension_by:   aus9 
Tags:           window manager wm
Comments:       Not the same as TCE for docs.
                This is for when you use the menu
                to click on help

                Or use your web browser to go to
                https://tinyurl.com/y5c29lo7


Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2020/07/22
 ' > $P-help.tcz.info

echo 'Title:          icewm-list.tcz
Description:    boot list for your bootloader
Version:        1.0
Author:         aus9
Original-site:  Tinycore
Copying-policy: opensource
Size:           4.0K
Extension_by:   aus9
Tags:           window manager wm
Comments:       Do NOT choose OnBoot in Apps please.
                Do not download this TCE unless you 
                have tested icewm and am happy to 
                have a boot loader list file to 
                automate the booting of IceWM.

                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 list
                (Maybe from onboot.lst?)

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

Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2020/07/22
 ' > $P-list.tcz.info

echo 'Title:          icewm-locale.tcz
Description:    locales 
Version:        1.7.0
Author:         see doc TCE for AUTHORS
Original-site:  https://github.com/bbidulock/icewm
Copying-policy: LGPL v2
Size:           492K
Extension_by:   aus9
Tags:           window manager wm locale
Comments:       locales untested by me
                
Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2020/07/22
  ' > $P-locale.tcz.info

echo 'Title:          icewm-themes.tcz
Description:    themes 
Version:        1.4
Author:         see doc TCE for AUTHORS
Original-site:  https://github.com/bbidulock/icewm
Copying-policy: LGPL v2
Size:           268K
Extension_by:   aus9
Tags:           window manager wm
Comments:       Extra themes to CrystalBlue and
                NanoBlue
                
Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2020/07/22
  ' > $P-themes.tcz.info

echo 'Title:          icewm.tcz
Description:    Stacking Window Manager 
Version:        1.7.0
Author:         see doc TCE for AUTHORS
Original-site:  https://github.com/bbidulock/icewm
Copying-policy: LGPL v2 
Size:           1.1M
Extension_by:   aus9
Tags:           window manager wm
Comments:       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 your other TCEs
                
                Most preferences, use the menu button or 
                context menu to change please. 
                 
                Please READ/edit your configs under ~/icewm
                Toolbar (and keys) has sakuara TCE set 
                sakura.tcz (forced dep as xterm set)
                pcmanfm.tcz (recommended TCE)
                geany.tcz (recommended TCE)
                keys names kb combos but only sakura is a dep
                (Means you need to load the others or add to boot list 
                to be effective)
                
                Feel free to edit YOUR configs. 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)
                See pcmanfm for a conflicting wallpaper method
                https://tinyurl.com/y3ouvb94

                After any change to a config file, any TCE added after boot,
                that has a kb combo set.....needs WM refresh with kb combo of
                Super+r = logo+r

                optional TCEs not already mentioned include: 
                icewm-themes (CrystalBlue is new default)
                icewm-help (stripped from main TCE)
                icewm-doc (man pages)
                icewm-locale 
                xscreensaver (and set local and root pw please)
                ( This evades lock workstation setting )
                
                Do NOT try logout/shutdown buttons after using 
                Ctrl+Alt+Del keys. Or any other non-exittc method.
                YOUR SYSTEM  WILL FREEZE!
                                
                Recommend https://ice-wm.org/FAQ/ too
                
Change-log:     2020/07/22 Original 1.7.0 on 11x
Current:        2020/07/22  
' > $P.tcz.info

echo 'icewm.tcz' > $P-locale.tcz.dep
echo 'icewm.tcz' > $P-help.tcz.dep
echo 'icewm.tcz' > $P-themes.tcz.dep
echo 'man-db.tcz' > $P-doc.tcz.dep
echo 'haveged.tcz
Xorg-7.7.tcz
sakura.tcz
adwaita-icon-theme.tcz' > $P.tcz.dep

submitqc --libs
