#!/bin/sh -e

##########################################################################
#   Description:
#       NetBSD desktop-installer
#
#   History:
#   Date        Name        Modification
#   2023-06-10  J Bacon     Begin
##########################################################################

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


##########################################################################
#   Function description:
#       Create a global Xsession file
#   Arguments:
#       
#   Returns:
#       
#   History:
#   Date        Name        Modification
#   2023-06-13  J Bacon     Begin
##########################################################################

create_xsession()
{
    if [ $# != 1 ]; then
	printf "Usage: create_xsession session-command\n"
	exit 1
    fi
    session_cmd=$1
    
    # FIXME: Use auto-something
    if [ ! -e $XSESSION.ctwm ]; then
	mv $XSESSION $XSESSION.ctwm
    fi
    
    # ck-launch-session may only be necessary for LXDE to enable reboot,
    # shutdown, etc when exiting the session
    if [ $de = lxde ]; then
	cat << EOM > $XSESSION
#!/bin/sh

exec ck-launch-session dbus-launch --exit-with-session $session_cmd
# exec ck-launch-session $session_cmd
EOM
    
    cat << EOM > $HOME/.xinitrc
#!/bin/sh -e

exec ck-launch-session dbus-launch --exit-with-session $session_cmd
# exec ck-launch-session $session_cmd
EOM
    else
	cat << EOM > $XSESSION
#!/bin/sh

exec $session_cmd
EOM
    
    cat << EOM > $HOME/.xinitrc
#!/bin/sh -e

exec $session_cmd
EOM
    fi

    auto-append-line 'DisplayManager*authName' \
	'DisplayManager*authName: MIT-MAGIC-COOKIE-1' $XDM_CONF nocomment
}


##########################################################################
#   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.  The pkgsrc tree should also be updated in case you
must install something from source, such as software than cannot
be redistributed in binary form, and hence has no binary package.

If you have updated recently using auto-update-pkgsrc, auto-update-system,
or pkgin upgrade and cvs update, then you can skip this step now.

EOM
printf "Update installed packages and pkgsrc tree? [y]/n "
read update
if [ 0"$update" != 0n ]; then
    auto-update-pkgsrc --defaults
fi

# Should have been installed as a dep, but just in case
pkgin -y install digest fetch git cvs gmake xz feh tk consolekit

if ! auto-package-installed security/mozilla-rootcerts-openssl; then
    printf "Install mozilla root certs? (Needed to access many sites) [y]/n "
    read mz
    if [ 0"$mz" != 0n ]; then
	pkgin -y install mozilla-rootcerts-openssl
    fi
fi

# Install sets only if running a RELEASE
# uname -r displays only X.Y
# For non-release versions, it displays something like 10.0_BETA
if uname -r | grep -E '^[0-9]+\.[0-9]+$'; then
    for base_set in comp games man modules text xbase xetc xcomp xfont xserver; do
	if [ ! -e /etc/mtree/set.$base_set ]; then
	    printf "Installing $set...\n"
	    auto-install-base-components $base_set
	else
	    printf "$base_set already installed.\n"
	fi
    done
else
    cat << EOM
    
Skipping installation of base sets.  If you don't have all you need,
you'll have to reinstall the OS.

EOM
fi

if [ ! -e /usr/pkgsrc ]; then
    printf "Installing pkgsrc tree...\n"
    cd /usr
    ftp ftp://ftp.netbsd.org/pub/pkgsrc/stable/pkgsrc.tar.xz
    tar -zxvf pkgsrc.tar.xz
fi

if [ ! -e /usr/pkgsrc/wip ]; then
    printf "Install pkgsrc-wip? y/[n] "
    read wip
    if [ 0"$wip" = 0y ]; then
	cd /usr/pkgsrc
	auto-pkgsrc-wip-checkout
    fi
fi

cat << EOM

1.. CTWM (NetBSD default)
2.. Gnome
3.. KDE
4.. LXDE
5.. LXQT
6.. MATE
7.. XFCE
8.. Custom

EOM
printf "Selection? "
read de_num

XDM_CONF=/etc/X11/xdm/xdm-config
XSESSION=/etc/X11/xdm/Xsession

de='unknown'
case $de_num in
1)
    # Install separately so user sees error messages
    if [ -e $XSESSION.ctwm ]; then
	mv -f $XSESSION.ctwm $XSESSION
    fi
    for pkg in evince xscreensaver; do
	pkgin -y install $pkg
    done
    rm $HOME/.xinitrc
    ;;

2)
    # Install separately so user sees error messages
    for pkg in evince gnome; do
	pkgin -y install $pkg
    done
    create_xsession gnome-session
    ;;

3)
    # Install separately so user sees error messages
    pkgin -y install kde
    create_xsession startkde
    ;;

4)
    # Install separately so user sees error messages
    # xscreensaver is used if present, but not a dep
    for pkg in evince xscreensaver lxde; do
	pkgin -y install $pkg
    done
    create_xsession startlxde
    ;;

5)
    # Install separately so user sees error messages
    for pkg in evince lxqt; do
	pkgin -y install $pkg
    done
    create_xsession startlxqt
    ;;

6)
    # Install separately so user sees error messages
    for pkg in evince mate; do
	pkgin -y install $pkg
    done
    create_xsession mate-session
    ;;

7)
    # Install separately so user sees error messages
    for pkg in evince xfce4; do
	pkgin -y install $pkg
    done
    create_xsession xfce4-session
    ;;

8)
    printf "Package name? (Use pkgin search to verify) "
    read package
    printf "Session command? (See category/$package/PLIST) "
    read session_cmd
    
    for pkg in xscreensaver $package; do
	pkgin -y install $pkg
    done
    create_xsession $session_cmd
    ;;
    
*)
    printf "Invalid selection.\n"
    exit 1
    ;;

esac

##########################################################################
#   From guide
##########################################################################

cp /usr/pkg/share/examples/rc.d/dbus /etc/rc.d
# A dbus child process is started by LXQT, but it is not sufficient
# to enable shutdown/reboot buttons on exit.  We apparently need a dbus
# service running as root.
auto-enable-service dbus $0

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

cat << EOM

Enabling XDM 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 xdm if not already running
if ! pgrep bin/xdm; then
    printf "Enable XDM graphical login? [y]/n "
    read xdm
    if [ 0"$xdm" != 0n ]; then
	auto-enable-service xdm $0
    fi
fi

# Enable devpubd-based external media mounting
# FIXME: Tolerate failure for now, since qmediamagager is
# difficult to install until a binary package exists
auto-automount-setup || true

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
    pkgin -y install $apps
fi

printf "Run auto-admin now to add users, etc? [y]/n "
read admin
if [ 0"$admin" != 0n ]; then
    auto-admin
fi
