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

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

P=vimb
SRC=$P-git
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc git webkitgtk-gtk3-dev"
for Z in $LIST 
    do
    su -c "tce-load -i $Z" $USER
done


cd /tmp
su -c "git clone  \
https://github.com/fanglingsu/$P.git" $USER
mv $P $SRC
cd $SRC
make -j4 # 4 seconds
make test # gives strange results like next line but claims pass
# (gtester:19335): CRITICAL **: 12:45:38.414: test_log_msg: assertion 'testcase_open == 0' failed / newline/ OK
make install DESTDIR=/tmp/$P # strip fails
cd /tmp

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

# main
######
strip --strip-unneeded $P/usr/local/bin/$P
strip --strip-unneeded $P/usr/local/lib/vimb/webext_main.so

# desktop - icon
################
# comes with no icon so I made one from mtpaint, get it from TC64 15 src when its accepted
# credit to GNUser for pointing out I had trailing space in desktop file
chown root:root $P.png
chmod 644 $P.png
mkdir -p $P/usr/local/share/pixmaps
cp $P.png $P/usr/local/share/pixmaps/

# some weirdness with existing desktop so start afresh....wbar is failing
rm -rf $P/usr/local/share/applications/*

echo '[Desktop Entry]
Encoding=UTF-8
Name=vimb
Comment=browse the web
Exec=vimb %U
Icon=vimb
Terminal=false
Type=Application
Categories=Network;Utility;WebBrowser;
MimeType=text/html;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https;
X-FullPathIcon=/usr/local/share/pixmaps/vimb.png' > $P/usr/local/share/applications/$P.desktop

chmod 644 $P/usr/local/share/applications/$P.desktop

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
DIR=/home/$USER/.config/vimb
FILE1=/usr/local/share/vimb/config
FILE2=/usr/local/share/vimb/style.css
FILE3=$DIR/config
FILE4=$DIR/style.css
[ -d $DIR ]   || mkdir -p $DIR
[ -f $FILE3 ] || cp $FILE1 $DIR/
[ -f $FILE4 ] || cp $FILE2 $DIR/
chmod 644 $DIR/*
chown $USER:staff -R $DIR
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v 3' > $P/usr/local/share/doc/$P/COPYING

# create files of which style.css is the only absolute needed file
mkdir -p $P/usr/local/share/$P	

cat >> $P/usr/local/share/$P/config <<'EOF'
# Homepage that vimb opens if started without a URI.
set home-page=https://www.duckduckgo.com/

# Path to the default download directory. If no download directory is set,
# download will be written into current directory. The following pattern will
# be expanded if the download is started '~/', '~user', '$VAR' and '${VAR}'.
set download-path=~/Downloads

# Command with placeholder '%s' called if form field is opened with $EDITOR to
# spawn the editor-like `x-terminal-emulator -e vim %s'. To use Gvim as the
# editor, it's necessary to call it with `-f' to run it in the foreground.
set editor-command=xterm -e "vi %s"

# If enabled the inputbox will be hidden whenever it contains no text.
set input-autohide=false

# Enable or disable the spell checking feature.
set spell-checking=false

# Set comma separated list of spell checking languages to be used for
# spell checking.
set spell-checking-languages=en,es

# Enable or disable support for WebGL on pages.
set webgl=true

# While typing a search command, show where the pattern typed so far matches.
set incsearch=true

# The font family to use as the default for content that does not specify a
# font.
set default-font=DejaVu Sans

# The font family used as the default for content using monospace font.
set monospace-font=DejaVu Sans Mono

# The font family used as the default for content using sans-serif font.
set sans-serif-font=DejaVu Sans

# The font family used as the default for content using serif font.
set serif-font=DejaVu Serif

# The default font size used to display text.
set font-size=16

# Default font size for the monospace font.
set monospace-font-size=13

# Default Full-Content zoom level in percent. Default is 100.
set default-zoom=120

# Shortcuts allow the opening of an URI built up from a named template with
# additional parameters.
shortcut-add duck=https://duckduckgo.com/?q=$0
shortcut-add d=http://dict.cc/?s=$0
shortcut-add g=https://encrypted.google.com/search?q=$0
shortcut-add y=http://www.youtube.com/results?search_query=$0
shortcut-add s=https://www.startpage.com/do/dsearch?query=$0

# Set the shortcut as the default, that is the shortcut to be used if no
# shortcut is given and the string to open is not an URI.
shortcut-default duck

# Map page zoom in normal mode to keys commonly used across applications
# + (zoom in), - (zoom out), = (zoom reset)
nmap + zI
nmap - zO
nmap = zz

# GUI color settings
# Color scheme: Base16 Eighties (https://github.com/chriskempson/base16)
set completion-css=color:#d3d0c8;background-color:#393939;font:12pt DejaVu Sans Mono;
set completion-hover-css=color:#d3d0c8;background-color:#393939;font:12pt DejaVu Sans Mono;
set completion-selected-css=color:#d3d0c8;background-color:#515151;font:12pt DejaVu Sans Mono;
set input-css=color:#d3d0c8;background-color:#393939;font:12pt DejaVu Sans Mono;
set input-error-css=color:#f2777a;background-color:#393939;font:12pt DejaVu Sans Mono;
set status-css=color:#ffcc66;background-color:#393939;font:12pt DejaVu Sans Mono;
set status-ssl-css=color:#99cc99;background-color:#393939;font:12pt DejaVu Sans Mono;
set status-ssl-invalid-css=color:#f2777a;background-color:#393939;font:12pt DejaVu Sans Mono;
EOF
cat >> $P/usr/local/share/$P/style.css <<'EOF'
*,div,pre,textarea,body,input,td,tr,p {
    background-color: #303030 !important;
    background-image: none !important;
    color: #bbbbbb !important;
}
h1,h2,h3,h4 {
    background-color: #303030 !important;
    color: #b8ddea !important;
}
a {
    color: #70e070 !important;
}
a:hover,a:focus {
    color: #7070e0 !important;
}
a:visited {
    color: #e07070 !important;
}
img {
    opacity: .5;
}
img:hover {
    opacity: 1;
}

/* Hint mode color styling
 * Color scheme: Base16 Eighties (https://github.com/chriskempson/base16)
 *
 * The precedence of the user style is lower than that of the website so you
 * have to mark your style definition to have higher priority.
 */
span[vimbhint^='label'] {
    background-color: #f2f0ec !important;
    border: 1px solid #2d2d2d !important;
    color: #2d2d2d !important;
    font: bold 10pt monospace !important;
    opacity: 1 !important;
    padding: .1em .4em !important;
    text-transform: uppercase !important;
}
span[vimbhint='label focus'] {
    font: bold 13pt monospace !important;
}
*[vimbhint^='hint'] {
    background-color: #ffcc66 !important;
    color: #2d2d2d !important;
}
*[vimbhint='hint focus'] {
    background-color: #6699cc !important;
    color: #2d2d2d !important;
}

EOF

# submitqc finds one perm
chmod 755 $P/usr/local/lib/vimb/webext_main.so
# believes libXt.so.6 libSM.so.6 libICE.so.6 are needed deps but they are only ldd deps

# TCZ them
#######  
LIST="$P $P-doc"
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:          vimb-doc.tcz
Description:    man page
Version:        git-20241028
Author:         Daniel Carl 
Original-site:  https://github.com/fanglingsu/vimb
Copying-policy: GPL v 3
Size:           20K
Extension_by:   aus9 @linuxquestions.org
Tags:           web browser 
Comments:       There is also a discussion on adblocking here
                https://fanglingsu.github.io/vimb/howto.html

Change-log:     2024/10/30 v git-20241028
Current:        2024/10/30       ' > $P-doc.tcz.info
echo 'man-db.tcz '    > $P-doc.tcz.dep

cat >> $P.tcz.info <<'EOF'
Title:          vimb.tcz
Description:    vim like web browser as below
Version:        git-20241028
Author:         Daniel Carl 
Original-site:  https://github.com/fanglingsu/vimb
Copying-policy: GPL v 3
Size:           84K
Extension_by:   aus9 @linuxquestions.org
Tags:           web browser 
Comments:       gtk3/gst/webkit web browser that has
                vim like key bindings and modes and
                supports mouse too.
                * Normal Mode -The default mode. 
                Pressing Escape always enter normal mode.
                * Input Mode
                Used for editing text elements in a webpage.
                * Command Mode
                Execute "ex" commands from the builtin inputbox (commandline).
                * Pass Through Mode
                Only the Esc and <C-[) keybindings are interpreted by Vimb, 
                all other keystrokes are given to the webview to handle them. This 
                allows the use of a website's configured keybindings, that
                might otherwise be swallowed by Vimb.   
                
                If you are mouse adverse you must read man page
                on Hinting. 

                I have made a home page and we start in input 
                mode at duckduckgo. 

                For new web address -> normal mode then input 
                shift+o <URL> 
                = capital Oh web address
                                
                Recommend you read the man page in vimb-doc. 
                Please change any setting you want at 
                $HOME/.config/vimb/config (and style.css)
                If you want sound, setup independently and 
                it should work when you launch vimb
 
                Altho 84K size looks good compare against
                totals for your other web browser due to deps              

Change-log:     2024/10/30 v git-20241028
Current:        2024/10/30       ' > $P.tcz.info
EOF

readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
#libwebkit2gtk-4.1.so.0]libjavascriptcoregtk-4.1.so.0]
#libgtk-3.so.0]#libgdk-3.so.0]gtk3 webkitgtk-gtk3
#libpangocairo-1.0.so.0]libpango-1.0.so.0]pango gtk3 webkitgtk-gtk3
#libharfbuzz.so.0]harfbuzz 
#libatk-1.0.so.0]at-spi2-core gtk3 webkitgtk-gtk3
#libcairo-gobject.so.2]libcairo.so.2]cairo pango gtk3 webkitgtk-gtk3
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2 gtk3 webkitgtk-gtk3
#libsoup-3.0.so.0]libsoup3 webkitgtk-gtk3
#libgmodule-2.0.so.0]libgio-2.0.so.0]#libgobject-2.0.so.0]libglib-2.0.so.0]glib2 at-spi2-core gtk3 webkitgtk-gtk3
readelf -d $P/usr/local/lib/vimb/webext_main.so | grep 'NEEDED' # no extras

# launching without gst* shows up errors for missing shared gst objects....font is set in config
# no icon cache update in install script

echo 'webkitgtk-gtk3.tcz
harfbuzz.tcz
gst-plugins-good.tcz
gst-plugins-bad.tcz
gst-libav.tcz
dejavu-fonts-ttf.tcz       ' > $P.tcz.dep

submitqc --libs --no-fix 
rm -rf *.zsync

