#!/bin/bash
PIS_SBIN="${PIS_SBIN:-/sbin}"
if [ -z "${PIS_LIBDIR:-}" ]; then
    case "$(uname -m)" in x86_64) PIS_LIBDIR=/usr/lib64/pis ;; *) PIS_LIBDIR=/usr/lib/pis ;; esac
fi
. "$PIS_LIBDIR/pis_lib" 2>/dev/null || . "$(dirname "$0")/pis_lib" || exit 1
pis_init || exit 1
items=()
for entry in "$(pis_path /etc/X11/xinit)"/xinitrc.*; do
    [ -f "$entry" ] || continue
    name="$(basename "$entry")"
    items+=("$name" "Installed X session")
done
[ "${#items[@]}" -gt 0 ] || { pis_error "No X sessions were found."; exit 1; }
choice=$(dialog --stdout --backtitle "$PIS_BACKTITLE" --title "Default X Session" \
    --menu "Select the default session. Current: $(pis_state_read gui_desktop)" 0 0 14 "${items[@]}") || exit 0
if pis_apply_desktop "$choice"; then
    pis_info "Default X Session" "'$choice' is now the system default for startx."
else
    pis_error "The default X session could not be changed."
    exit 1
fi
