#!/bin/bash
set -e

# rice-xfce.sh: makes xfce look good
# v1.3 (2020-05-08), ed <irc.rizon.net>, MIT-licensed
# https://ocv.me/dev/?rice-xfce.sh

[ $(id -u) -eq 0 ] && {
	echo as regular user please
	exit 1
}

# given a list of themes, select the first theme which is installed
sel() {
	dest=$1
	shift
	while [ ! -z "$1" ]; do
		IFS=' ' read $dest pkg <<< "$1"
		shift
		have_pkg "$pkg" && return
		printf "\033[1;33mwanted to use $dest '\033[37m${!dest}\033[33m' but the package '\033[37m$pkg\033[33m' is not installed\033[0m\n"
	done
	printf "\033[1;31mcould not find any $dest package to use\033[0m\n"
	declare $dest=
	false
}

# download a file to stdout
dl() {
	command -v wget >/dev/null && { wget -O- "$1"; return $?; }
	command -v curl >/dev/null && { curl "$1"; return $?; }
	printf "\033[1;31mneed wget or curl\033[0m\n"
	false
}

dist=
[ -e /etc/apk ] && dist=alpine
[ -e /etc/apt ] && dist=debian
[ -e /etc/yum.conf ] && dist=centos
[ -e /etc/pacman.conf ] && dist=arch
[ -z "$dist" ] && {
	echo "unsupported distro"
	exit 1
}

[ $dist = debian ] && {
	have_pkg() {
		dpkg --get-selections | grep -qE "^$pkg\b"
	}
	sel icons \
		"Papirus-Dark papirus-icon-theme" \
		"Adwaita adwaita-icon-theme"
	sel xfwm \
		"Adapta-Nokto adapta-gtk-theme" \
		"Arc arc-theme" \
		"Breeze xfwm4-theme-breeze" \
		"Shiki-Colors-Easy-Metacity shiki-colors-xfwm-theme" \
		"Kokodi xfwm4"
	sel gtk \
		"Adapta-Nokto-Eta adapta-gtk-theme" \
		"Shiki-Brave shiki-colors" \
		"Adwaita gnome-themes-extra"
	
	echo "using icons = $icons"
	echo "using xfwm = $xfwm"
	echo "using gtk = $gtk"
}

[ $dist = centos ] && {
	rpm -qa > /dev/shm/pkgs.got
	have_pkg() {
		grep -qE "^$pkg\b" /dev/shm/pkgs.got
	}
	sel icons \
		"Paper-Mono-Dark paper-icon-theme" \
		"Adwaita adwaita-icon-theme"
	sel xfwm \
		"Adapta-Nokto adapta-gtk-theme" \
		"Agualemon xfwm4-themes"
	sel gtk \
		"Adapta-Nokto-Eta adapta-gtk-theme" \
		"Adwaita adwaita-gtk2-theme"
	
	echo "using icons = $icons"
	echo "using xfwm = $xfwm"
	echo "using gtk = $gtk"
}

[ $dist = arch ] && {
	# TODO detection
	xfwm=Adapta-Nokto
	icons=Papirus-Dark
	gtk=Adapta-Nokto-Eta
}

[ $dist = alpine ] && {
	# defaults are sane
	true
}

# delete old panels and plugins
xfconf-query -c xfce4-panel -p /panels -rR
xfconf-query -c xfce4-panel -p /plugins -rR
#xfconf-query -c xfce4-panel -p /panels
#xfconf-query -c xfce4-panel -p /plugins -lv

# create plugins
[ $dist = arch ] &&
	xfconf-query -c xfce4-panel -p /plugins/plugin-1 -n -t string -s whiskermenu ||
	xfconf-query -c xfce4-panel -p /plugins/plugin-1 -n -t string -s applicationsmenu
xfconf-query -c xfce4-panel -p /plugins/plugin-2 -n -t string -s tasklist
xfconf-query -c xfce4-panel -p /plugins/plugin-3 -n -t string -s separator
xfconf-query -c xfce4-panel -p /plugins/plugin-4 -n -t string -s systray
xfconf-query -c xfce4-panel -p /plugins/plugin-5 -n -t string -s pulseaudio
xfconf-query -c xfce4-panel -p /plugins/plugin-6 -n -t string -s power-manager-plugin
xfconf-query -c xfce4-panel -p /plugins/plugin-7 -n -t string -s clock

[ $dist = arch ] ||
	xfconf-query -c xfce4-panel -p /plugins/plugin-1/show-button-title -n -t bool -s false
xfconf-query -c xfce4-panel -p /plugins/plugin-2/flat-buttons   -n -t bool   -s true
xfconf-query -c xfce4-panel -p /plugins/plugin-2/sort-order     -n -t int    -s 4
xfconf-query -c xfce4-panel -p /plugins/plugin-3/expand         -n -t bool   -s true
xfconf-query -c xfce4-panel -p /plugins/plugin-3/style          -n -t int    -s 0
xfconf-query -c xfce4-panel -p /plugins/plugin-4/show-frame     -n -t bool   -s false
xfconf-query -c xfce4-panel -p /plugins/plugin-4/size-max       -n -t int    -s 48
xfconf-query -c xfce4-panel -p /plugins/plugin-7/digital-format -n -t string -s " %a %Y-%m-%d  %H:%M:%S "
xfconf-query -c xfce4-panel -p /plugins/plugin-7/timezone       -n -t string -s Europe/Oslo

xfconf-query -c xfce4-panel -p /panels                          -n -t int    -s 1 -a
xfconf-query -c xfce4-panel -p /panels/panel-1/length           -n -t int    -s 100
xfconf-query -c xfce4-panel -p /panels/panel-1/mode             -n -t int    -s 0
xfconf-query -c xfce4-panel -p /panels/panel-1/position         -n -t string -s 'p=8;x=0;y=0'
xfconf-query -c xfce4-panel -p /panels/panel-1/position-locked  -n -t bool   -s true
xfconf-query -c xfce4-panel -p /panels/panel-1/size             -n -t int    -s 28

ids=({1..5})
upower -e | grep -q battery && ids+=(6)
ids+=(7)

xfconf-query -c xfce4-panel -p /panels/panel-1/plugin-ids \
  $(printf ' -n -t int -s %d' ${ids[@]})

xfconf-query -c keyboards -p /Default/KeyRepeat/Delay -n -t int    -s 250
xfconf-query -c keyboards -p /Default/KeyRepeat/Rate  -n -t int    -s 30
xfconf-query -c xfwm4     -p /general/wrap_windows    -n -t bool   -s false
xfconf-query -c xfwm4     -p /general/wrap_workspaces -n -t bool   -s false
xfconf-query -c xfwm4     -p /general/use_compositing -n -t bool   -s false

[ -z "$icons" ] || xfconf-query -c xsettings -p /Net/IconThemeName -n -t string -s $icons
[ -z "$xfwm"  ] || xfconf-query -c xfwm4     -p /general/theme     -n -t string -s $xfwm
[ -z "$gtk"   ] || xfconf-query -c xsettings -p /Net/ThemeName     -n -t string -s $gtk

xfconf-query -c xsettings -p /Xft/Antialias           -n -t int    -s 1
xfconf-query -c xsettings -p /Xft/Hinting             -n -t int    -s 1
xfconf-query -c xsettings -p /Xft/HintStyle           -n -t string -s hintfull

for a in custom default; do xfconf-query -c xfce4-keyboard-shortcuts -p "/commands/$a/<Primary><Alt>t" -nt string -s 'xfce4-terminal --maximize'; done
xfconf-query -c xfce4-keyboard-shortcuts -p '/commands/custom/<Primary>KP_Multiply' -nt string -s 'pactl set-sink-volume 0 -2%'
xfconf-query -c xfce4-keyboard-shortcuts -p '/commands/custom/<Primary>KP_Subtract' -nt string -s 'pactl set-sink-volume 0 +2%'
xfconf-query -c xfce4-keyboard-shortcuts -p '/commands/custom/<Alt><Shift>f' -nt string -s 'urxvt -geometry 169x50 -bl -sb -e ranger'
xfconf-query -c xfce4-keyboard-shortcuts -p '/commands/custom/<Alt><Primary><Shift>f' -nt string -s 'urxvt -geometry 169x50 -bl -sb -e sudo ranger'

mkdir -p ~/.config/xfce4/terminal/
cat > ~/.config/xfce4/terminal/terminalrc <<'EOF'
[Configuration]
MiscMenubarDefault=FALSE
ScrollingLines=65533
ScrollingOnOutput=FALSE
FontUseSystem=TRUE
ColorBackground=#222222
ColorCursor=#4e4e9a9a0606
ColorSelection=#35536b
ColorPalette=#404040;#f03669;#b8e346;#ffa402;#02a2ff;#f65be3;#3da698;#d2d2d2;#606060;#c75b79;#c8e37e;#ffbe4a;#71cbff;#b67fe3;#9cf0ed;#ffffff
ShortcutsNoMnemonics=TRUE
ShortcutsNoHelpkey=TRUE
ShortcutsNoMenukey=TRUE
MiscShowUnsafePasteDialog=FALSE
MiscSlimTabs=TRUE
EOF

printf 'set %s=\n' mouse ttymouse > ~/.vimrc

mkdir -p ~/.config/geany/
cat >~/.config/geany/geany.conf <<'EOF'
[geany]
check_detect_indent=true
detect_indent_width=true
show_indent_guide=true
long_line_color=#888888
line_wrapping=true
msgwindow_visible=false

[plugins]
active_plugins=/usr/lib/geany/overview.so;/usr/lib/geany/splitwindow.so;
EOF

cat >~/.config/geany/keybindings.conf <<'EOF'
menu_redo=<Primary><Shift>z
EOF


# (mkdir -p ~/.themes && cd ~/.themes && git clone https://github.com/EliverLara/Juno.git)


# try to use bundled local copies of the dotfiles, retrieve from ocv.me if not present
[ -e ~/.tmux.conf ] ||
for f in .{bashrc,Xresources,tmux.conf}; do
	[ -e $f ] &&
		cp -pv $f ~/$f &&
		continue
	
	dl https://ocv.me/dot/$f > ~/x &&
		mv ~/x ~/$f ||
		break
done

sed -ri 's/:Meslo LG S DZ:/:Monospace:/' ~/.Xresources &&
	xrdb ~/.Xresources || true

mkdir -p ~/bin
echo '#!/bin/bash' > ~/bin/xfce-onboot.sh

[ $dist = arch ] && false && (
	tee -a ~/bin/xfce-onboot.sh <<'EOF'
# fix mouse accel
xinput list | awk '/slave\s+pointer/ && !/XTEST/ {sub(/\t[^=]+$/,"");sub(/.*=/,"");print}' | while IFS= read -r id; do xinput --list-props $id; xinput --set-prop $id 'libinput Accel Speed' -0.37; xinput --set-prop $id 'libinput Accel Profile Enabled' 0, 1; done
EOF
)

chmod 755 ~/bin/xfce-onboot.sh
mkdir -p ~/.config/autostart
cat > ~/.config/autostart/xfce-onboot.desktop <<EOF
[Desktop Entry]
Type=Application
Name=xfce-onboot
Exec=$HOME/bin/xfce-onboot.sh
EOF

~/bin/xfce-onboot.sh

xfce4-panel -r

printf '\n  close all shells then press CTRL-ALT-T\n\n'
exit 0



xfconf-query | awk '/^  / {print $1}' | while IFS= read -r x; do xfconf-query -c "$x" -lv | sed -r "s@^@$x@"; done > a
> keyboard-layout/Default/XkbDisable  false
> keyboard-layout/Default/XkbLayout   us
> keyboard-layout/Default/XkbVariant  altgr-intl
