#!/bin/bash

W_MANAGERS="fluxbox icewm jwm"
VARS="ICONS GTK_THEME GTK3_THEME FLUX_THEME ICEWM_THEME TRAY_ICON CURSOR_THEME WALLPAPER GEANY_THEME"

        DARK_ICONS="papirus-antix"
    DARK_GTK_THEME="Arc-Dark"
   DARK_FLUX_THEME="/usr/share/fluxbox/styles/BlueDay-Medium"
  DARK_ICEWM_THEME="BlueDay-Medium/default.theme"
    DARK_TRAY_ICON="Black Gnome"
 DARK_CURSOR_THEME="DMZ-Black"
    DARK_WALLPAPER="/usr/share/wallpaper/pylons.jpg"
   DARK_GEANY_THEME="zenburn.conf"

       LIGHT_ICONS="papirus-antix"
   LIGHT_GTK_THEME="Arc-EvoPro2"
  LIGHT_GTK3_THEME="Arc-EvoPro2"
  LIGHT_FLUX_THEME="/usr/share/fluxbox/styles/Radiant-antiX-Medium"
 LIGHT_ICEWM_THEME="Clearview Blue Medium/default.theme"
   LIGHT_TRAY_ICON="Black Gnome"
LIGHT_CURSOR_THEME="DMZ-Black"
   LIGHT_WALLPAPER="/usr/share/wallpaper/pylons.jpg"
 LIGHT_GEANY_THEME="zenburn.conf"

ROX_DIR=".config/rox.sourceforge.net/ROX-Filer"
ROOT_DIR="/root"

ME=${0##*/}
DIR=$HOME
PRETEND=
VERBOSE=
NO_RESTART=

usage() {
    local ret=${1:-0}
    cat <<Usage
Usage: $ME [options] <light|dark>

Set the desktop theme to either light or dark.

Options:
    -d --dir=<dir>   Modify files under <dir> instead of HOME
    -h --help        Show this help
    -n --no-restart  Don't restart the window manager
    -p --pretend     Show the changes instead of doing theme
    -q --quiet       Suppress warning messages
    -r --root        Change some files under /root
    -v --verbose     Show the changes and do them
Usage

    exit $ret
}

main() {
    local args="$*"

    [ $# -eq 0 ] && usage

    local arg val="unknown"
    while [ $# -gt 0 -a -n "$1" -a -z "${1##-*}" ]; do
        arg=${1#-}
        shift

        case $arg in
            -dir|[d])
                [ $# -lt 1 ] && fatal "Expected a parameter after: -$arg"
                val=$1
                [ -n "$val" -a -z "${val##-*}" ] \
                    && fatal "Suspicious argument after -$arg: $val"
                shift           ;;

            *=*) val=${arg#*=} ;;
            *)   val="unknown" ;;
        esac

        case $arg in
             -dir|d) DIR=$val                        ;;
            -help|h) usage                           ;;
      -no-restart|n) NO_RESTART=true                 ;;
         -pretend|p) PRETEND=echo                    ;;
           -quiet|q) QUIET=true                      ;;
            -root|r) AS_ROOT=true                    ;;
         -verbose|v) VERBOSE=true                    ;;
                  *) error "Unknown argument: -$arg" ;;
        esac
    done

    [ $# -eq 1 ] || fatal "Expected a single command line argument"

    local theme=$1
    case $theme in
         dark)  THEME=DARK  ;;
        light)  THEME=LIGHT ;;
            *)  fatal "Unrecognized theme: $theme" ;;
    esac

    local var
    for var in $VARS; do
        eval $var=\$${THEME}_$var
    done

    if [ "$AS_ROOT" ]; then
        test -w $ROOT_DIR || fatal "Can't write to $ROOT_DIR"
        replace_line "s|^\s*(gtk-theme-name=)(\"?).*\2|\1\2$GTK_THEME\2|" $ROOT_DIR/.gtkrc-2.0
        replace_line "s|^\s*(gtk-theme-name=)(\"?).*\2|\1\2$GTK3_THEME\2|" $ROOT_DIR/.config/gtk-3.0/settings.ini
        #replace_line "s|^\s*(gtk-cursor-theme-name=)(\"?).*\2\s*|\1\2$CURSOR_THEME\2|" $ROOT_DIR/.config/gtk-3.0/settings.ini
        cursor_file="/usr/share/icons/$CURSOR_THEME/cursor.theme"
        test -e $cursor_file && pretend ln -sf $cursor_file /etc/alternatives/x-cursor-theme
        exit 0
    fi

    replace_line "s|^\s*(Theme=\")[^\"]*(\").*|\1$ICEWM_THEME\2|"                   .icewm/theme
    replace_line "s|^\s*(session.styleFile:\s+).*|\1$FLUX_THEME|"                   .fluxbox/init
    replace_line "s|^(\s*<Option name=\"icon_theme\">)[^<]*(</Option>)|\1$ICONS\2|" $ROX_DIR/Options
    replace_line "s|^\s*(gtk-theme-name=)(\"?).*\2|\1\2$GTK_THEME\2|"               .gtkrc-2.0
    replace_line "s|^\s*(gtk-icon-theme-name=)(\"?).*\2\s*|\1\2$ICONS\2|"           .gtkrc-2.0
    replace_line "s|^\s*(gtk-theme-name=)(\"?).*\2|\1\2$GTK_THEME\2|"               .config/gtk-3.0/settings.ini
    replace_line "s|^\s*(gtk-icon-theme-name=)(\"?).*\2\s*|\1\2$ICONS\2|"           .config/gtk-3.0/settings.ini

    #replace_line "s|^\s*(gtk-cursor-theme-name=)(\"?).*\2\s*|\1\2$CURSOR_THEME\2|"  .gtkrc-2.0
    #replace_line "s|^\s*(gtk-cursor-theme-name=)(\"?).*\2\s*|\1\2$CURSOR_THEME\2|"  .config/gtk-3.0/settings.ini

    replace_line "s|^\s*(theme=\s*).*|\1$TRAY_ICON|" .config/volumeicon/volumeicon

    if test -r "$WALLPAPER"; then
        replace_line "s|^\s*([a-z-]+=).*|\1$WALLPAPER|"      .desktop-session/wallpaper-list.conf
        replace_line "s|^\s*(DEFAULT=\s*).*|\1$WALLPAPER|"   .desktop-session/wallpaper.conf
        replace_line "s|^\s*(wallpaper=\s*).*|\1$WALLPAPER|" .config/spacefm/session
        replace_line "s|^\s*(wallpaper=\s*).*|\1$WALLPAPER|" .config/zzzfm/session
        replace_line_wm "s|^(\s*<backdrop[^>]*>)[^<]*(</backdrop>).*|\1$WALLPAPER\2|" .config/rox.sourceforge.net/ROX-Filer/pb_antiX-%s
    fi

    #test geany
    replace_line "s|^\s*(color_scheme=\s*).*|\1$GEANY_THEME|" .config/geany/geany.conf

    replace_file $theme .dillo/style.css
    replace_file $theme .jwm/theme
    replace_file $theme $ROX_DIR/Options

    [ "$DISPLAY" -a $UID != 0 ] && gksu "$0 --root $args"

    [ "$PRETEND" ] && return

    restart_desktop
}

replace_line() {
    local regex=$1 file=$2
    [ -z "${file##/*}" ] || file=$DIR/$file
    test -e "$file" || return

    if [ "$PRETEND" -o "$VERBOSE" ]; then
        echo -en "file $file:\n  "
        sed -n -r "$regex"p "$file"
    fi

    [ "$PRETEND" ] && return
    sed -i -r "$regex" "$file"
}

replace_file() {
    local theme=$1  file=$2
    [ -z "${file##/*}" ] || file=$DIR/$file
    local new=$file.$theme

    if ! test -e "$new"; then
        warn "Missing file $new"
        return
    fi

    local type=$(stat -c %F "$file" 2>/dev/null)
    case $type in
        sym*) cmd="ln -sf" ; new=$(basename $new) ;;
           *) cmd="cp -af"                        ;;
    esac

    if [ "$PRETEND" -o "$VERBOSE" ]; then
        echo $cmd $new $file
    fi

    [ "$PRETEND" ] && return
    $cmd $new $file
}

replace_line_wm() {
    local file regex=$1 spec=$2
    while read file; do
        [ -z "$file" ] && continue
        replace_line "$regex" "$file"
    done<< Wm_Files
$(wm_files $spec)
Wm_Files
}

wm_files() {
    local wm str=$1
    for wm in $W_MANAGERS; do
        printf "$str\n" $wm
    done
}

pretend() {
    if [ "$PRETEND" -o "$VERBOSE" ]; then
        echo "$@"
    fi
    [ "$PRETEND" ] && return
    "$@"
}

restart_desktop() {
    local restart=/usr/local/lib/desktop-session/desktop-session-restart

    [ "$NO_RESTART"    ] && return
    [ "$DISPLAY"       ] || return
    [ "$DIR" = "$HOME" ] || return
    [ $UID -eq 0       ] && return
    [ -x $restart      ] || return

    $restart
}

warn() {
    [ "$QUIET" ] && return
    echo "$ME warning: $*" >&2
}

fatal() {
    echo "$ME Error: $*" >&2
    exit 3
}


main "$@"

exit 0
