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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti "

P=rofi
V=1.7.5+wayland3
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc meson cmake git wayland-dev libxcb-dev glib2-dev pango-dev \
libxkbcommon-dev gdk-pixbuf2-dev startup-notification-dev imagemagick     "
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/lbonn/$P/releases/download/1.7.5%2Bwayland3/$SRC.tar.xz  " $USER
tar xvf $P*xz
cd $SRC
mkdir build && cd build
meson setup --prefix=/usr/local -Dstrip=true -Ddebug=false -Dbuildtype=plain 
# Subprojects
# libgwater : YES
# libnkutils: YES
# User defined options
# buildtype : plain
# debug     : false
# prefix    : /usr/local
# strip     : true
# to show settings as I could have used
meson configure # extra to above
# drun true Desktop file mode 
# wayland enabled Build with wayland support 
# xcb  enabled  Build with X11/xcb support
ninja # 5 seconds
DESTDIR=/tmp/$P ninja install

# reduce deps by not using SVG
cd /tmp/$P/usr/local/share/icons/hicolor/scalable/apps/
convert *.svg -resize 48x48 $P.png
rm -rf *.svg

cd /tmp/$P/usr/local/share/rofi/themes
rm -rf iggy*     # remove large theme
# some themes have fonts lets fix now
sed 's|Source Code Pro|Sans|g' -i arthur.rasi
sed 's|FontAwesome 22|Sans 18|g' -i arthur.rasi # button
sed 's|Sans 8|Sans 12|'        -i arthur.rasi
sed 's|Sans 10|Sans 12|'       -i arthur.rasi
sed 's|Topaz-8 10|Sans 12|'    -i c64.rasi
sed 's|Times New Roman|Sans|'  -i dmenu.rasi
sed 's|mono 18|Sans 12|'       -i docu.rasi
sed 's|monospace|Sans|'       -i docu.rasi # inputbar
cd /tmp

# no dev
rm -rf $P/usr/local/include
rm -rf $P/usr/local/lib

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

# theme TCE
############ 
# easier to move all themes and add just one back
mkdir -p $P-theme-selector/usr/local/bin
mv $P/usr/local/bin/$P-theme-selector $P-theme-selector/usr/local/bin/
mkdir -p $P-theme-selector/usr/local/share/applications
mv $P/usr/local/share/rofi/ $P-theme-selector/usr/local/share

DESK1=$P-theme-selector.desktop
mv $P/usr/local/share/applications/$DESK1 $P-theme-selector/usr/local/share/applications
echo 'X-FullPathIcon=/usr/local/share/pixmaps/rofi.png' >> $P-theme-selector/usr/local/share/applications/$DESK1
# NOTE rofi is a dep of the theme TCE

# main
#######
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/COPYING $P/usr/local/share/doc/$P/COPYING

# add 1 theme back into main but setup for install script
mkdir -p $P/usr/local/share/$P/files
mv $P-theme-selector/usr/local/share/$P/themes/fancy.rasi $P/usr/local/share/$P/files

# desktop stuff
#############
mkdir -p $P/usr/local/share/pixmaps
mv $P/usr/local/share/icons/hicolor/scalable/apps/rofi.png $P/usr/local/share/pixmaps/
mkdir -p $P/usr/local/share/icons/hicolor/48x48/apps
rm -rf $P/usr/local/share/icons/hicolor/scalable
cp $P/usr/local/share/pixmaps/$P.png $P/usr/local/share/icons/hicolor/48x48/apps/

DESK2=$P.desktop
echo 'X-FullPathIcon=/usr/local/share/pixmaps/rofi.png' >> $P/usr/local/share/applications/$DESK2
sed 's|rofi -show|rofi -show drun -show-icons|' -i $P/usr/local/share/applications/$DESK2

# config 
######### obtained by reading the man pages -> $ rofi -dump-config > config.rasi
# modified to force theme to fancy, local file not ramdrive file

cat >> $P/usr/local/share/$P/files/config.rasi <<'EOF'
configuration {
/*	modes: "window,drun,run,ssh";*/
/*	font: "Sans 18";*/
/*	location: 0;*/
/*	yoffset: 0;*/
/*	xoffset: 0;*/
/*	fixed-num-lines: true;*/
/*	show-icons: false;*/
/*	terminal: "rofi-sensible-terminal";*/
/*	ssh-client: "ssh";*/
/*	ssh-command: "{terminal} -e {ssh-client} {host} [-p {port}]";*/
/*	run-command: "{cmd}";*/
/*	run-list-command: "";*/
/*	run-shell-command: "{terminal} -e {cmd}";*/
/*	window-command: "wmctrl -i -R {window}";*/
/*	window-match-fields: "all";*/
	icon-theme: "hicolor";
/*	drun-match-fields: "name,generic,exec,categories,keywords";*/
/*	drun-categories: ;*/
/*	drun-show-actions: false;*/
/*	drun-display-format: "{name} [<span weight='light' size='small'><i>({generic})</i></span>]";*/
/*	drun-url-launcher: "xdg-open";*/
/*	disable-history: false;*/
/*	ignored-prefixes: "";*/
/*	sort: false;*/
/*	sorting-method: "normal";*/
/*	case-sensitive: false;*/
/*	cycle: true;*/
/*	sidebar-mode: false;*/
/*	hover-select: false;*/
/*	eh: 1;*/
/*	auto-select: false;*/
/*	parse-hosts: false;*/
/*	parse-known-hosts: true;*/
/*	combi-modes: "window,run";*/
/*	matching: "normal";*/
/*	tokenize: true;*/
/*	m: "-5";*/
/*	filter: ;*/
/*	dpi: -1;*/
/*	threads: 0;*/
/*	scroll-method: 0;*/
/*	window-format: "{w}    {c}   {t}";*/
/*	click-to-exit: true;*/
/*	global-kb: false;*/
/*	max-history-size: 25;*/
/*	combi-hide-mode-prefix: false;*/
/*	combi-display-format: "{mode} {text}";*/
/*	matching-negate-char: '-' /* unsupported */;*/
/*	cache-dir: ;*/
/*	window-thumbnail: false;*/
/*	drun-use-desktop-cache: false;*/
/*	drun-reload-desktop-cache: false;*/
/*	normalize-match: false;*/
/*	steal-focus: false;*/
/*	application-fallback-icon: ;*/
/*	refilter-timeout-limit: 300;*/
/*	xserver-i300-workaround: false;*/
/*	completer-mode: "recursivebrowser";*/
/*	pid: "/home/tc/.cache/rofi.pid";*/
/*	display-window: ;*/
/*	display-windowcd: ;*/
/*	display-run: ;*/
/*	display-ssh: ;*/
/*	display-drun: ;*/
/*	display-combi: ;*/
/*	display-keys: ;*/
/*	display-filebrowser: ;*/
/*	display-recursivebrowser: ;*/
/*	kb-primary-paste: "Control+V,Shift+Insert";*/
/*	kb-secondary-paste: "Control+v,Insert";*/
/*	kb-secondary-copy: "Control+c";*/
/*	kb-clear-line: "Control+w";*/
/*	kb-move-front: "Control+a";*/
/*	kb-move-end: "Control+e";*/
/*	kb-move-word-back: "Alt+b,Control+Left";*/
/*	kb-move-word-forward: "Alt+f,Control+Right";*/
/*	kb-move-char-back: "Left,Control+b";*/
/*	kb-move-char-forward: "Right,Control+f";*/
/*	kb-remove-word-back: "Control+Alt+h,Control+BackSpace";*/
/*	kb-remove-word-forward: "Control+Alt+d";*/
/*	kb-remove-char-forward: "Delete,Control+d";*/
/*	kb-remove-char-back: "BackSpace,Shift+BackSpace,Control+h";*/
/*	kb-remove-to-eol: "Control+k";*/
/*	kb-remove-to-sol: "Control+u";*/
/*	kb-accept-entry: "Control+j,Control+m,Return,KP_Enter";*/
/*	kb-accept-custom: "Control+Return";*/
/*	kb-accept-custom-alt: "Control+Shift+Return";*/
/*	kb-accept-alt: "Shift+Return";*/
/*	kb-delete-entry: "Shift+Delete";*/
/*	kb-mode-next: "Shift+Right,Control+Tab";*/
/*	kb-mode-previous: "Shift+Left,Control+ISO_Left_Tab";*/
/*	kb-mode-complete: "Control+l";*/
/*	kb-row-left: "Control+Page_Up";*/
/*	kb-row-right: "Control+Page_Down";*/
/*	kb-row-up: "Up,Control+p";*/
/*	kb-row-down: "Down,Control+n";*/
/*	kb-row-tab: "";*/
/*	kb-element-next: "Tab";*/
/*	kb-element-prev: "ISO_Left_Tab";*/
/*	kb-page-prev: "Page_Up";*/
/*	kb-page-next: "Page_Down";*/
/*	kb-row-first: "Home,KP_Home";*/
/*	kb-row-last: "End,KP_End";*/
/*	kb-row-select: "Control+space";*/
/*	kb-screenshot: "Alt+S";*/
/*	kb-ellipsize: "Alt+period";*/
/*	kb-toggle-case-sensitivity: "grave,dead_grave";*/
/*	kb-toggle-sort: "Alt+grave";*/
/*	kb-cancel: "Escape,Control+g,Control+bracketleft";*/
/*	kb-custom-1: "Alt+1";*/
/*	kb-custom-2: "Alt+2";*/
/*	kb-custom-3: "Alt+3";*/
/*	kb-custom-4: "Alt+4";*/
/*	kb-custom-5: "Alt+5";*/
/*	kb-custom-6: "Alt+6";*/
/*	kb-custom-7: "Alt+7";*/
/*	kb-custom-8: "Alt+8";*/
/*	kb-custom-9: "Alt+9";*/
/*	kb-custom-10: "Alt+0";*/
/*	kb-custom-11: "Alt+exclam";*/
/*	kb-custom-12: "Alt+at";*/
/*	kb-custom-13: "Alt+numbersign";*/
/*	kb-custom-14: "Alt+dollar";*/
/*	kb-custom-15: "Alt+percent";*/
/*	kb-custom-16: "Alt+dead_circumflex";*/
/*	kb-custom-17: "Alt+ampersand";*/
/*	kb-custom-18: "Alt+asterisk";*/
/*	kb-custom-19: "Alt+parenleft";*/
/*	kb-select-1: "Super+1";*/
/*	kb-select-2: "Super+2";*/
/*	kb-select-3: "Super+3";*/
/*	kb-select-4: "Super+4";*/
/*	kb-select-5: "Super+5";*/
/*	kb-select-6: "Super+6";*/
/*	kb-select-7: "Super+7";*/
/*	kb-select-8: "Super+8";*/
/*	kb-select-9: "Super+9";*/
/*	kb-select-10: "Super+0";*/
/*	kb-entry-history-up: "Control+Up";*/
/*	kb-entry-history-down: "Control+Down";*/
/*	ml-row-left: "ScrollLeft";*/
/*	ml-row-right: "ScrollRight";*/
/*	ml-row-up: "ScrollUp";*/
/*	ml-row-down: "ScrollDown";*/
/*	me-select-entry: "MousePrimary";*/
/*	me-accept-entry: "MouseDPrimary";*/
/*	me-accept-custom: "Control+MouseDPrimary";*/
  timeout {
      action: "kb-cancel";
      delay:  0;
  }
  filebrowser {
      directories-first: true;
      sorting-method:    "name";
  }
  icon {
      theme: "hicolor";
  }
}

@theme "/home/tc/.config/rofi/themes/fancy.rasi"

EOF


# install script
################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
gtk-update-icon-cache -q -f -t /usr/local/share/icons/hicolor
USER=`cat /etc/sysconfig/tcuser`

LOOP1=/tmp/tcloop/rofi/usr/local/share/rofi/files/config.rasi
HC=/home/$USER/.config/rofi
[ -d $HC ] || mkdir -p $HC
if [ ! -f $HC/config.rasi ] ; then
   cp $LOOP1 $HC/
   chmod 644 $HC/config.rasi
fi

LOOP2=/tmp/tcloop/rofi/usr/local/share/rofi/files/fancy.rasi
HT=/home/$USER/.config/rofi/themes
[ -d $HT ] || mkdir -p $HT
if [ ! -f $HT/fancy.rasi ] ; then
   cp $LOOP2 $HT/
   chmod 644 $HT/fancy.rasi
fi
chown -R $USER:staff $HC
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed


# TCZ them
#######
LIST="$P $P-doc $P-theme-selector"
for Z in $LIST
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:          rofi-doc.tcz 
Description:    man pages
Version:        1.7.5+wayland3
Author:         https://github.com/lbonn/rofi/blob/wayland/AUTHORS
Original-site:  https://github.com/lbonn/rofi
Copying-policy: Accompanied 
Size:           48K
Extension_by:   aus9 @linuxquestions.org
Tags:           launcher 
Comments:       Check file list, 
                run $ man <name>

Change-log:     2024/08/27 v 1.7.5+wayland3 on 15x
Current:        2024/08/27     ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          rofi.tcz 
Description:    libX11/XWayland launcher and misc tool as below
Version:        1.7.5+wayland3
Author:         https://github.com/lbonn/rofi/blob/wayland/AUTHORS
Original-site:  https://github.com/lbonn/rofi
Copying-policy: Accompanied 
Size:           236K
Extension_by:   aus9 @linuxquestions.org
Tags:           launcher 
Comments:       Fork of https://github.com/davatorium/rofi
                An X11 & (X)wayland pop-up window switcher, run dialog, 
                dmenu replacement. It supports keyboard & mouse 
                navigation, type to filter, tokenized search 
                and log into a remote machine via ssh. (Untested)
                
                config = $HOME/.config/rofi/config.rasi
                You should correct pathway to theme if not user=tc
                PLUS you have 1xtheme file
                $HOME/.config/rofi/themes/fancy.rasi
                which is called by last line in your config file
                
                NORMAL USE: 
                If not using wbar icon, run 
                $ rofi -show drun -show-icons
                Scroll thru selections via arrow key or mouse
                Press enter against selection to open it
                If App is already open elsewhere, you may switch to it 
                --- subject to WM settings
                Press Escape key to exit app if no selection made.
                OR mouse click outside app to exit
                (change config for click-to-exit if you like app open if 
                 you need to swap workspaces for a while)
                Highly recommend you create a keybinding for above in WM,
                if WM allows kb shortcuts

                Selection + Enter key opens "target app" and closes rofi

                If your WM supports workspaces, $ rofi -show window
                provides a list of what is open on each workspace

                To run it like dmenu (exe search not a desktop file search) 
                $ rofi -show run 
                but note the following enhancements, IMHO,
                string search is anywhere in exe name
                eg search for "gr" without quotes has hits including
                addgroup, groff,  vigr etc

                fancy theme includes tabs for windows etc
 
                TESTED basic functions on icewm/xorg and sway/xwayland 
                rofi on wayland likely to bork if xwayland not loaded and enabled 
                See rofi-doc & rofi-theme-selector                   
               
Change-log:     2024/08/27 v 1.7.5+wayland3 on 15x
Current:        2024/08/27     ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' 
#libglib-2.0.so.0]libgmodule-2.0.so.0]libgio-2.0.so.0]libgobject-2.0.so.0]glib2-cairo->pango
#libcairo.so.2]cairo->pango
#libpango-1.0.so.0]libpangocairo-1.0.so.0]pango
#libxkbcommon.so.0]libxkbcommon
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2->gtk-update-icon-cache
#libxcb.so.1]libxcb-util.so.1]libxcb-xkb.so.1]libxkbcommon-x11.so.0]PLUS
#libxcb-ewmh.so.2]libxcb-icccm.so.4]libxcb-randr.so.0]libxcb-cursor.so.0]PLUS
#libxcb-xinerama.so.0]libxcb->libxkbcommon
#libstartup-notification-1.so.0]startup-notification
#libwayland-client.so.0]libwayland-cursor.so.0]wayland

echo 'pango.tcz
libxkbcommon.tcz
gtk-update-icon-cache.tcz
startup-notification.tcz
wayland.tcz ' > $P.tcz.dep

echo 'Title:          rofi-theme-selector.tcz 
Description:    GUI theme selector for rofi
Version:        1.7.5+wayland3
Author:         https://github.com/lbonn/rofi/blob/wayland/AUTHORS
Original-site:  https://github.com/lbonn/rofi
Copying-policy: Accompanied 
Size:           32K
Extension_by:   aus9 @linuxquestions.org
Tags:           launcher 
Comments:       This TCE uses same icon as main TCE
                rofi defaults to fancy theme.  

                If you like to view others, load this TCE and 
                press enter against each theme, except dmenu.
                Once you are happy with a theme, press Alt + a to accept
                new selection. Or press Escape to keep current theme.
                Use wbar or run 
                $ rofi-theme-selector

                Alt + a edits config to usr/share/* pathway So...
                Copy any new theme from tcloop/TCE/usr/local/share/rofi/themes/
                -> $HOME/.config/rofi/themes

                Independent themes can also be used see
                https://github.com/newmanls/rofi-themes-collection
                and other like minded sites 
                TC has a man-page for this TCE too 

                Be aware the following have font settings which over rule config
                arthur, c64, dmenu & docu
                
                The last line of your config should read as <change name of theme>
                and pathway
                @theme "/home/tc/.config/rofi/themes/<new theme>.rasi"
                If you lack confidence to do this, keep this TCE loading which has
                a dep of rofi                             
                
Change-log:     2024/08/27 v 1.7.5+wayland3 on 15x -1xlarge theme & desktop file removed
Current:        2024/08/27     ' > $P-theme-selector.tcz.info

# exe is a bash script
echo 'rofi.tcz
bash.tcz
coreutils.tcz
sed.tcz ' > $P-theme-selector.tcz.dep

submitqc --libs
rm -rf *.zsync

