#!/bin/sh

set -eu

. /usr/share/geordi/_functions
. /etc/geordi/scripts-config

trap 'cd "$(pwd)"' EXIT

RT=${RT:-"/var/run/geordi/rt"}
CONFIG_DIR="${CONFIG_DIR:-/etc/geordi}"
AUTO_RECONNECT="${AUTO_RECONNECT:-0}"
ALWAYS_REFRESH_CHROOT="${ALWAYS_REFRESH_CHROOT:-1}"
DEBUG=${DEBUG:-0}
INFO=${INFO:-1}

cd "${CONFIG_DIR}"

case "$(echo "${ALWAYS_REFRESH_CHROOT}" | tr A-Z a-z)" in
	yes|true|on|y|1)
		info "Refreshing chroot."
		info "Tip: to avoid refreshing every time, edit ALWAYS_REFRESH_CHROOT in /etc/geordi/scripts-config."
		/usr/sbin/geordi-refresh-chroot
		;;
esac

if [ ! -d "${RT}" ];
then
	error "${RT} does not exist - run /usr/sbin/geordi-refresh-chroot"
fi

debug "Starting geordi-local"

if [ -x /usr/bin/rlwrap ];
then
	/usr/bin/rlwrap /usr/lib/geordi/geordi-local
else
	/usr/lib/geordi/geordi-local
fi
