#!/bin/sh
# This script produces an /etc/rc file with some configurable changes

# The build system screws up the values of string variables so that they
# include explicit quotes; undo this:
for var in `printenv | sed -n 's@=".*"$@@p'`; do
  eval "$var=`printenv $var`"
  [ x"`printenv $var`" = x"none" ] && eval unset "$var"
done

# Switch to section $1
section () { SEC="$1"; }
# If in a different section than before, output an appropriate comment
output_section_header () {
  if [ "$SEC" != "$OLD_SEC" ]; then
    [ "$OLD_SEC" ] && echo ''
    echo "# $SEC"
  fi
  OLD_SEC="$SEC"
}

# This just outputs the command string given by the args, preceded by an
# `echo' of the same, and possibly by a section header.
# Only the first _arg_ is echoed though, which can be used if the command
# is complicated to simplify the output.
init_cmd () { output_section_header; echo echo "init: $1";  echo "$*"; }
# Like init_cmd, but doesn't echo anything
invis_init_cmd () { output_section_header; echo "$*"; }
# Output a variable assignment setting $1 to $2, possibly preceded by a
# section header.
param () { output_section_header; echo "$1=\"$2\""; PARAMS="$PARAMS $1"; }


#### parameters

section params

# hostname
[ "$CONFIG_HOSTNAME" ] && param HOST "$CONFIG_HOSTNAME"

# Network params
if [ "$CONFIG_NET_ADDR" ]; then
  NET="`echo "$CONFIG_NET_ADDR" | sed 's@\.[^.]*$@@'`"
  param ADDR "$CONFIG_NET_ADDR"
  param INTERFACE "${CONFIG_NET_INTERFACE:-eth0}"
  param NETMASK "${CONFIG_NET_NETMASK:-255.255.255.0}"
  param BROADCAST "${CONFIG_NET_BROADCAST:-$NET.255}"
  [ "$CONFIG_NET_GATEWAY" ] && param GATEWAY "$CONFIG_NET_GATEWAY"
fi

# NFS root param
[ "$CONFIG_NFSROOT" ] && param NFSROOT "$CONFIG_NFSROOT"

# Export parameters
[ "$PARAMS" ] && echo "export$PARAMS"


#### setup

if [ "$CONFIG_NFS_ROOT_TREE" != y ]; then
  # A normal host
  section hostname
  [ "$CONFIG_HOSTNAME" ] && init_cmd 'hostname "$HOST"'

  section mounts
  init_cmd 'mount -t proc proc /proc'
  init_cmd 'mount -t ramfs ram /var'

  section temp_dirs
  invis_init_cmd 'for VD in tmp log run lock; do'
  invis_init_cmd '  echo init: mkdir "/var/$VD"'
  invis_init_cmd '  mkdir "/var/$VD"'
  invis_init_cmd 'done'
  init_cmd 'chmod 01777 /var/tmp'

  if [ "$CONFIG_NET_ADDR" ]; then
    section net
    init_cmd 'ifconfig lo 127.0.0.1'
    init_cmd 'route add -net 127.0.0.0 netmask 255.0.0.0 lo'
    init_cmd 'ifconfig "$INTERFACE" "$ADDR" netmask "$NETMASK" broadcast "$BROADCAST"'
    init_cmd 'route add "$ADDR" "$INTERFACE"'
    [ "$CONFIG_NET_GATEWAY" ] && init_cmd 'route add default gw "$GATEWAY"'
  fi

  if [ "$CONFIG_NFSROOT" ]; then
    section nfsroot
    init_cmd portmap '&'
    invis_init_cmd ''  # necessary for some bizarre reason
    invis_init_cmd 'for MP in bin etc usr home lib; do'
    invis_init_cmd '  echo init: mount "$NFSROOT/$MP" "/$MP"'
    invis_init_cmd '  mount "$NFSROOT/$MP" "/$MP"'
    invis_init_cmd 'done'
  fi
fi

section daemons
[ "$CONFIG_USER_THTTPD_THTTPD" = y ] && init_cmd thttpd "-c '/cgi-bin/*' &"
[ "$CONFIG_USER_INETD_INETD" = y ] && init_cmd inetd '&'

section epilogue
if [ "$CONFIG_NFSROOT" ]; then
  invis_init_cmd '# chain to the rc file from NFS'
  invis_init_cmd '. /etc/rc'
else
  invis_init_cmd "echo ''"
  invis_init_cmd 'cat /etc/motd'
  invis_init_cmd "echo ''"
  invis_init_cmd 'cat /etc/version'
fi
