# fss-0005 fss-000d iki-0000
#
# Example initfile based on the init/entries/default.entry and many of its related files.
#

entry/settings:
  pid ready
  show init

  control init.socket
  control_user 0
  control_group 0
  control_mode ug+rwx,o-rwx

entry/main:
  timeout start 7
  timeout stop 7
  timeout kill 3

  failsafe maintenance

  item boot
  item console

entry/boot:
  start boot root require
  start boot proc asynchronous require
  start boot devices asynchronous require
  start boot file_system asynchronous
  start boot modules wait

  ready

entry/console:
  start service mouse asynchronous

  start terminal two asynchronous
  start terminal three asynchronous
  start terminal four asynchronous
  start terminal one require wait

entry/maintenance:
  # TODO: Support IKI variables for certain env data, such as an architecture path (thus '/bin[environment:"architecture_path"]/bash'). with some compilation built ins, or maybe have settings support calling programs to populate variables, like '/bin[variable:"architecture_path"]/bash'.
  # TODO: There could even be settings, entries, and exits to support for calling and processing custom, named, functions built into the source code.
  execute /bin/bash --login

rule/boot/devices/settings:
  name "Setup /dev file system"

  on start need boot root

rule/boot/devices/script:
  start {
    main() {
      local IFS=$' \t\n' # Prevent IFS exploits by overriding with a local scope.

      if [[ ! -d /dev/pts ]] ; then
        mkdir /dev/pts
      fi

      if [[ ! -d /dev/shm ]] ; then
        mkdir /dev/shm
      fi

      return 0
    \}

    main ${*}
  }

rule/boot/devices/command:
  start mount /dev/pts
  stop umount -l /dev/pts

rule/boot/devices/command:
  start mount /dev/shm
  stop umount -l /dev/shm

rule_boot_file_system_settings:
  name "Setup Filesystem"

  on start need boot root
  on start need boot proc
  on start need boot devices

  on stop need boot proc
  on stop need boot devices

rule/boot/file_system/command:
  start mount -n -a -O no_netdev
  stop umount -n -arf -O no_netdev

rule/boot/file_system/command:
  start swapon -a
  stop swapoff -a

rule/boot/file_system/script:
  start {
    main() {
      local IFS=$' \t\n' # Prevent IFS exploits by overriding with a local scope.

      if [[ ! -d /var/run/init ]] ; then
        mkdir /var/run/init
      fi
    \}

    main ${*}
  }

rule/boot/modules/settings:
  name "Setup Kernel Modules"

  on start need boot root
  on start need boot proc
  on start want boot filesystem

rule/boot/modules/script:
  start {
    main() {
      local IFS=$' \t\n' # Prevent IFS exploits by overriding with a local scope.

      if [[ ! -f /proc/modules ]] ; then
        exit 0
      fi

      if [[ -d /modules ]] ; then
        if [[ ! -e /modules/$(uname -r)/modules.dep ]] ; then
          depmod
        else
          depmod -A
        fi
      fi

      return 0
    \}

    main ${*}
  }

rule/boot/proc/settings:
  name "Setup /proc Filesystem"

  on start need boot root

rule/boot/proc/command:
  start mount /proc

rule/boot/proc/script:
  start {
    main() {
      local IFS=$' \t\n' # Prevent IFS exploits by overriding with a local scope.

      if [[ -d /proc/bus/usb ]] ; then
        mount /proc/bus/usb
      fi

      return 0
    \}

    main ${*}
  }

  stop {
    main() {
      local IFS=$' \t\n' # Prevent IFS exploits by overriding with a local scope.

      if [[ -d /proc/bus/usb ]] ; then
        umount -l /proc/bus/usb
      fi

      return 0
    \}

    main ${*}
  }

rule/boot/root/settings:
  name "Setup Root Filesystem"

rule/boot/root/command:
  start mount -o remount,rw /

rule/boot/root/script:
  start {
    main() {
      local IFS=$' \t\n' # Prevent IFS exploits by overriding with a local scope.
      local i=

      for i in /dev /dev/pts /dev/shm /firmware /mnt /modules /proc /sys /tmp /var /var/log /var/run /var/tmp ; do
        if [[ ! -d ${i} ]] ; then
          mkdir ${i}
        fi
      done

      return 0
    \}

    main ${*}
  }

rule/service/mouse/settings:
  name "Console Mouse"
  capability all=
  nice 15

rule/service/mouse/script:
  start {
    main() {
      local IFS=$' \t\n' # Prevent IFS exploits by overriding with a local scope.

      # This works if gpm service is run as root, but if not then this should be in a separate rule file with appropriate access to write to /var/run (don't forget to chown!).
      if [[ ! -d /var/run/mouse/ && -d /var/run ]] ; then
        mkdir /var/run/mouse/
      fi
    \}

    main ${*}
  }

rule/service/mouse/service:
  pid_file /var/run/mouse/mouse.pid

  start gpm -m variable:"device" -t variable:"protocol" variable:"options"

rule/terminal/one/settings:
  name "System Terminal 1"

rule/terminal/one/command:
  start agetty -8 -i -J - linux

  rerun start success delay 1000 reset
  rerun start failure delay 5000

rule/terminal/two/settings:
  name "System Terminal 2"

rule/terminal/two/command:
  start agetty -8 tty2 linux

  rerun start success delay 1000 reset
  rerun start failure delay 5000 max 100

rule/terminal/three/settings:
  name "System Terminal 3"

rule/terminal/three/command:
  start agetty -8 tty3 linux

  rerun start success delay 1000 reset
  rerun start failure delay 5000 max 100

rule/terminal/four/settings:
  name "System Terminal 4"

rule/terminal/four/command:
  start agetty -8 tty4 linux

  rerun start success delay 1000 reset
  rerun start failure delay 5000 max 100
