#!/bin/sh

# Copyright (c) 2014-2015 Franco Fichtner <franco@opnsense.org>
# Copyright (c) 2004-2011 Scott Ullrich <sullrich@gmail.com>
# Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>
# All rights reserved.

# make sure the user can't kill us
trap : 2
trap : 3

# If recovery console shell option has been specified
if [ -f "/tmp/donotbootup" ]; then
	rm /tmp/donotbootup
	/bin/csh
	/usr/local/etc/rc.reboot
	# no coming back from here
fi

# shell started with parameters, passthrough to real shell
if [ -n "${*}" ]; then
	/bin/csh "${@}"
	exit ${?}
fi

# endless loop
while : ; do

echo

# If we are on the install media advertise that fact
if _tmpdir=$(mktemp -d -q /.diskless.XXXXXX); then
	rmdir ${_tmpdir}
else
	option99="99) Launch the installer"
fi

echo " 0) Logout                             7) Ping host"
echo " 1) Assign Interfaces                  8) Shell"
echo " 2) Set interface(s) IP address        9) pfTop"
echo " 3) Reset the root password           10) Filter Logs"
echo " 4) Reset to factory defaults         11) Restart web interface"
echo " 5) Power off system                  12) Upgrade from console"
echo " 6) Reboot system                     13) Restore a configuration"
if [ -n "${option99}" ]; then
# flat for alignment...
echo "                                      ${option99}"
fi

echo
read -p "Enter an option: " OPCODE
echo

# see what the user has chosen
case ${OPCODE} in
0|exit|logout|quit)
        exit
        ;;
1)
        /usr/local/etc/rc.initial.setports
        ;;
2)
        /usr/local/etc/rc.initial.setlanip
        ;;
3)
        /usr/local/etc/rc.initial.password
        ;;
4)
        /usr/local/etc/rc.initial.defaults
        ;;
5)
        /usr/local/etc/rc.initial.halt
        ;;
6)
        /usr/local/etc/rc.initial.reboot
        ;;
7)
        /usr/local/etc/rc.initial.ping
        ;;
8)
        /bin/csh
        ;;
9)
        /usr/local/sbin/pftop
        ;;
10)
	/usr/sbin/tcpdump -s 256 -v -S -l -n -e -ttt -i pflog0
	;;
11)
	/usr/local/etc/rc.restart_webgui
	;;
12)
	/usr/local/etc/rc.initial.firmware
	;;
13)
	/usr/local/etc/rc.restore_config_backup
	;;
99)
	/usr/local/etc/rc.installer
	;;
*)
	/bin/sh -c "${OPCODE}"
	;;
esac

/usr/local/etc/rc.initial.banner

done
