#!/bin/sh -e

##########################################################################
#   Description:
#       OpenBSD desktop-installer
#
#   History:
#   Date        Name        Modification
#   2024-06-25  izzy Meyer  Begin Work on OpenBSD Support
#   2024-10-21  izzy Meyer  Add auto-admin functionality
##########################################################################

usage()
{
    printf "Usage: $0\n"
    exit 1
}


##########################################################################
#   Function description:
#       Create a global Xsession file
#   Arguments:
#       
#   Returns:
#       
#   History:
#   Date        Name         Modification
#   2024-06-25  izzy Meyer   Remove COOKIE stuff from NetBSD
##########################################################################

create_xsession()
{
    if [ $# != 1 ]; then
	printf "Usage: create_xsession session-command\n"
	exit 1
    fi
    session_cmd=$1

    cat << EOM > $XSESSION
#!/bin/sh

exec $session_cmd
EOM
}


##########################################################################
#   Main
##########################################################################

if [ $# != 0 ]; then
    usage
fi

cat << EOM

It is best to have all installed packages up-to-date before installing
more packages, in case not every dependency version is checked
correctly.

If you have updated recently using pkg_add -u, then you can skip
this step now.

EOM
printf "Update installed packages? [y]/n "
read update
if [ 0"$update" != 0n ]; then
    pkg_add -u 
fi

# Should have been installed as a dep, but just in case
pkg_add dbus avahi git gmake xz feh tk-8.6.13 consolekit2

cat << EOM

1.. FVWM (OpenBSD default)
2.. CWM (OpenBSD alternate)
3.. LXQT
4.. MATE
5.. XFCE
6.. Custom

EOM
printf "Selection? "
read de_num

XSESSION=/etc/X11/xenodm/Xsession

de='unknown'
case $de_num in
1)
    pkg_add xscreensaver
    create_xsession fvwm
    ;;

2)
    pkg_add xscreensaver
    create_xsession cwm
    ;;

3)
    # Install separately so user sees error messages
    for pkg in evince--light lxqt lxqt-extras; do
        pkg_add $pkg
    done
    create_xsession startlxqt
    ;;

4)
    # Install separately so user sees error messages
    for pkg in evince--light mate mate-extras; do
        pkg_add $pkg
    done
    create_xsession "ck-launch-session mate-session"
    ;;

5)
    # Install separately so user sees error messages
    for pkg in evince--light xfce xfce-extras; do
        pkg_add $pkg
    done
    create_xsession xfce4-session
    ;;

6)
    printf "Package name? (Use pkg_info -Q to verify) "
    read package
    printf "Session command? (Use pkg_info -M & the pkg-readme at /usr/local/share/doc/pkg-readmes to verify) "
    read session_cmd

    for pkg in xscreensaver $package; do
	pkg_add $pkg
    done
    create_xsession $session_cmd
    ;;

*)
    printf "Invalid selection.\n"
    exit 1
    ;;

esac

##########################################################################
#   From Guide
##########################################################################

# Enable multicast (cannot be done with auto-enable-service as this is an option, not a service)
rcctl enable multicast
# Enable dbus rc service
auto-enable-service messagebus $0
# Enable avahi (needed for network mounts, trash://, and computer://)
auto-enable-service avahi_daemon $0
# Order them in the right order as avahi_daemon needs dbus running to work
rcctl order messagebus avahi_daemon


printf "Enable apm power manager daemon? (useful on laptops) [y]/n "
read apm
if [ 0"apm" != 0n ]; then
    auto-enable-service apmd $0
fi

# Enhanced XDM with background and restart/shutdown/restart-x11 buttons
auto-replace-file /usr/local/share/desktop-installer/XenoDM/Xsetup_0 /etc/X11/xenodm/Xsetup_0
auto-replace-file /usr/local/share/desktop-installer/XenoDM/GiveConsole /etc/X11/xenodm/GiveConsole

cat << EOM

Enabling XenoDM will cause the screen to switch to graphics mode.

If you are running desktop-installer on the primary text console,
you can return to the text console by typing Ctrl+Alt+F1 on most
systems (or Meta-key + F1 under VirtualBox).

EOM
# Offer to enable xenodm if not already running
if ! pgrep bin/xenodm; then
    printf "Enable XenoDM graphical login? [y]/n "
    read xenodm
    if [ 0"$xenodm" != 0n ]; then
        auto-enable-service xenodm $0
    fi
fi

for app in firefox thunderbird keepassxc; do
    if ! auto-package-installed $app; then
	printf "Install $app? y/[n] "
	read install
	if [ 0"$install" = 0y ]; then
	    apps="$apps ${app}"
	fi
    fi
done
if [ ! -z "$apps" ]; then
    pkg_add $apps
fi
