#!/bin/ash
# <lessdisks_path>/etc/init.d/lessdisks

# copyright 2004 vagrant@freegeek.org, distributed under the terms of the
# GNU General Public License version 2 or any later version.

# this script populates the /tmp (read-writeable) directory with needed
# directories.
# it also creates links for system-specific files, based on MAC 
# address. this must be run after the /tmp directory is mounted, as the 
# filesystem is read-only otherwise.

# make a symbolic link from 
# <diskless_path>/etc/rcS.d/S19lessdisks -> ../init.d/lessdisks
# for proper execution at boot time...

# may be used with either plip or a non-pcmcia ethernet card

# source config file, ash style
. /etc/lessdisks/server.config

# TODO have it scan for available filesystem types if 
# rwfilesystem is empty, and mount that instead...

case "$1" in
  start) ;;
  stop) ;;
  restart) ;;
  force-reload) ;;
esac

if [ -z "$(mount | egrep devfs)" ]; then
  mount -t devfs devfs /dev
  /etc/init.d/devfsd start
fi

if [ -z "$rw" ]
then
  rw="/var/state/lessdisks/"
fi

case "$rwfilesystem" in
  ramdisk) /sbin/mkfs.ext2 -b 1024 /dev/ram0
    mount -t ext2 /dev/ram0 $rw
    echo "mounting /dev/ram0 on $rw"
    ;;  
  ramfs) mount -t ramfs -o rw $rw
    echo "mounting ramfs on $rw"
    ;;
  tmpfs)  mount -t tmpfs -o rw,size=$tmpfs_size tmpfs $rw
    echo "mounting tmpfs on $rw"
    ;;
  translucency) ;;
  *)echo "no valid filesystem was found to mount on!"
    echo "this will cause big problems!"
    echo " "
    echo "edit /etc/lessdisks/server.config"
    echo "to include a valid value for rwfilesystem"
    echo " "
    ;;
esac

if [ "$rwfilesystem" = "translucency" ] ; then
  # requires the translucency kernel module
  # makes / appear to be read-writeable...
  echo "mounting translucency filesystem..."
  /sbin/modprobe translucency
  # translucency requires tmp
  mount -t tmpfs tmpfs /tmp
  # need /etc for lessdisks...
  mkdir -p /var/state/lessdisks/etc
  echo "/ -> /var/state/lessdisks" > /proc/sys/translucency/0
else
  echo "Populating $rw"

  mkdir -p $rw/etc

  for d in $copy_dirs $rw_dirs; do
    if [ -d /$d ]; then
      mkdir -p $rw/$d
    fi
  done

  # TODO make tarcopy function
  if [ -n "$rw_dirs" ]; then
    # preserve directory structure, though not data.
    cd $rw
    tar --no-recursion -cpf - $(find $rw_dirs -type d) | tar xpf -
    cd -
  else
    echo "WARNING: no writeable directories defined..."
  fi

  if [ -n "$copy_dirs" ]; then
    # preserve data and directory structure
    cd $rw
    tar -cpf - $copy_dirs | tar xpf -
    cd -
  fi

  for d in $rw_dirs $copy_dirs; do
    if [ -d /$d ] && [ -d $rw/$d ]; then
      # bind one part of filesystem onto another
      # "man mount" for more info.
      mount --bind -t bind $rw/$d /$d
    fi
  done

fi

# generates the MAC address
macaddress=`ifconfig | grep HWaddr | awk '{print $5}'`

# support for user-mode-linux- requires eth0 to be
# initialized
if [ "" = "$macaddress" ]; then
  macaddress=`ifconfig eth0 | grep HWaddr | awk '{print $5}'`
fi
# support for non-networked variations of lessdisks
if [ "" = "$macaddress" ]; then
  echo "no mac address found."
  echo "this is likely to be a problem,"
  echo "unless this is a non-network variation of  lessdisks"
  echo "such as a cdrom-based lessdisks"
  macaddress="no:mac:address:found"
fi

echo "mac address is $macaddress"

echo "macaddress=$macaddress" >> $rw/etc/config

# turns the MAC address into a freindlier name...
# be sure to edit /etc/lessdisks/terminals.config with the appropriate MAC 
# address and hostname
plip=$(ifconfig | egrep plip)
if [ -z "$plip" ]; then
  hostname=`grep $macaddress /etc/lessdisks/terminals.config | awk '{print $1}'`
else
  # plip interfaces may have the same macaddress, so choose...
  availablenames=`grep $macaddress /etc/lessdisks/terminals.config | awk '{print $1}'`
  echo "Which computer are you using?"
  echo "hit enter for default or a new computer"
  echo "$availablenames"
  read hostname
fi

# test if $hostname is empty
# TODO include variation for plip...
if [ "" = "$hostname" ]; then 
  hostname="default"
fi

echo "hostname is $hostname"
echo $hostname > $rw/etc/hostname
echo "hostname=$hostname" >> $rw/etc/config

# load modules if defined
# modules which require additional options should use commas:
# i.e.: "sb io=0x200" should be listed as "sb,io=0x220"
for module in $modules ; do
  module=$(echo $module | tr "," " ")
  modprobe $module
done

# get a value for the ip address
if [ -z "$plip" ]; then
  ipaddress=`ifconfig | grep -A 1 eth | grep inet | awk -F ":" '{print $2}' | awk '{print $1}'`
else
  ipaddress=`ifconfig plip0 | grep inet | awk -F ":" '{print $2}' | awk '{print $1}'`
fi

echo "ipaddress=$ipaddress" >> $rw/etc/config

echo "127.0.0.1 localhost" > $rw/etc/hosts
echo "$ipaddress $hostname" >> $rw/etc/hosts
# grab ip address of disk server using awk
disk_server_ip=$(awk -Faddr= '/\/ nfs/{print $2}' /proc/mounts | awk '{print $1}' | awk -F , '{print $1}' | head -n 1)
# perl way to do the same...
#perl -n -e 'm,/ nfs.*?addr=([\d.]+), && printf("%s\n", $1)' < /proc/mounts | head -1
if [ -n "$disk_server_ip" ]; then
  echo "$disk_server_ip disk $disk_server" >> $rw/etc/hosts
  echo "disk_server_ip=$disk_server_ip" >> $rw/etc/config
else
  echo "WARNING: no ip address found for disk server!"
fi

# add entries from hosts.static
# used for hostnames which remain static

if [ -r /etc/lessdisks/hosts.static ]; then
  cat /etc/lessdisks/hosts.static >> $rw/etc/hosts
fi

# add entries from the hosts.<ipnetwork> file for machines
# on the same network (deprecated)

ipnetwork=`echo "$ipaddress" | cut -d "." -f 1,2,3`

if [ -r /etc/lessdisks/hosts.$ipnetwork ] && [ -n "$ipnetwork" ]; then
  cat /etc/lessdisks/hosts.$ipnetwork >> $rw/etc/hosts
fi

# add entries in the hosts.extra file, only actually including 
# entries with similar ip addresses

if [ -r /etc/lessdisks/hosts.extra ] && [ -n "$ipnetwork" ]; then
  # grab host entries as whitespace seprated lines
  # ip address and hostname(s) are comma-separated
  # i.e. "ip1,hostname1,hostname2 ip2,hostname3,hostname4"
  all_lines=$(awk -F "#" '{print $1}' /etc/lessdisks/hosts.extra | sort -n -u | tr -s "\t" " " | tr -s " " ",")

  for line in $all_lines; do
    iprange=$(echo $a | cut -d . -f 1,2,3)
    if [ "$ipnetwork" = "$iprange" ]; then
      # translate commas back into spaces, and stick in the hosts file
      echo $line | tr "," " " >> $rw/etc/hosts
    fi
  done
fi

##### export filesystem if hostname is default

if [ "default" = "$hostname" ]; then
  . /usr/sbin/lessdisks-export
fi

# run terminal-specific rc scripts, such as rc.xterminal
if [ -d /usr/share/lessdisks/terminal-types/ ]
then
  for role in /usr/share/lessdisks/terminal-types/rc.*
  do
    . $role
  done
fi
