#!/bin/sh

# copyright 2004-2005 Vagrant Cascadian <vagrant@freegeek.org>, distributed
# under the terms of the GNU General Public License version 2 or any later
# version.

# only run if package is still installed
test -f /usr/share/initrd-netboot-tools/version || exit 0

if [ -r /etc/initrd-netboot/initrd-netboot.conf ]; then
  . /etc/initrd-netboot/initrd-netboot.conf
fi

# TODO: get these values from /etc/initrd-netboot/initrd-netboot.conf ?
CONFDIR="/etc/initrd-netboot"

script_dir="$CONFDIR/install_scripts/"

if [ -z "$INITRDDIR" ]; then
  echo "INITRDDIR not defined, exiting..."
  exit 1
fi

if [ ! -d "$script_dir" ]; then
  echo "no script directory found! $script_dir"
  exit 1
fi

version="$(cat /usr/share/initrd-netboot-tools/version)"
# generate configuration file
initrd_netboot_tools_conf=$INITRDDIR/$CONFDIR/initrd-netboot-tools.conf
mkdir -p $(dirname $initrd_netboot_tools_conf)
echo CONFDIR="$CONFDIR" > $initrd_netboot_tools_conf
echo script_dir="$script_dir" >> $initrd_netboot_tools_conf
echo initrd_netboot_version=$version >> $initrd_netboot_tools_conf

# add script, which sources other scripts
mkdir -p $INITRDDIR/scripts
cp $CONFDIR/initrd-netboot-tools $INITRDDIR/scripts/20initrd-netboot-tools

# set defaults for exe and files
if [ -z "$initrd_exe" ]; then
  initrd_exe="/usr/bin/cut /sbin/ifconfig /bin/grep /usr/bin/head /usr/bin/tr /sbin/route /sbin/udhcpc" 
fi
if [ -z "$initrd_files" ]; then
  initrd_files="/dev/ttyS0 /dev/console /dev/urandom"
fi

if [ "true" = "$discover_probe" ] && [ -r /usr/share/discover/pci.lst ]; then
  # FIXME: also handle pci-26.lst and /etc/lessdisks/discover1.custom
  discover_files=""
  discover_modules=""
  for a in /usr/share/discover/pci.lst /usr/share/discover/pci-26.lst /etc/lessdisks/discover1.custom ; do
    if [ -f "$a" ]; then
      discover_files="$discover_files $a"
      discover_modules="$discover_modules $(grep ethernet /usr/share/discover/pci.lst | awk '{print $3}' | sort -u)"
    fi
  done
fi

# TODO copy and install module dependencies 

copy_initrd_file(){
  filename="$1"
  mkdir -p "$(dirname $INITRDDIR/$filename)"
  cp -a "$filename" "$INITRDDIR/$filename"
}

# hard-code these two, so old debconf values don't bite us
initrd_files="$initrd_files $CONFDIR/network_script $CONFDIR/initrd-netboot.conf"

for a in $initrd_exe $initrd_files $discover_files $(run-parts --test $script_dir) ; do
  # need -a in case device nodes are used.
  copy_initrd_file $a
  if [ -L "$a" ]; then
    # also copy symlink
    # TODO: check recursively for symlinks
    copy_initrd_file "$(readlink $a)" 
  fi
done

if [ -r /etc/lessdisks/terminal_chroot.conf ]; then
  . /etc/lessdisks/terminal_chroot.conf
  echo lessdisks_path="\"$lessdisks_path\"" >> "$INITRDDIR/$CONFDIR/initrd-netboot.conf"
  echo chroot_name="\"$chroot_name\"" >> "$INITRDDIR/$CONFDIR/initrd-netboot.conf"
fi

# ldd resolution code taken from /usr/share/initrd-tools/e2fsprogs
for i in `ldd $initrd_exe | sort -u | awk '{print $3}'` 
do
	mkdir -p `dirname $INITRDDIR/$i`
	cp $i $INITRDDIR/$i
done

if [ -z "$MODULEDIR" ]; then
  echo "MODULEDIR not defined, exiting..."
  exit 1
fi

if [ -r "$MODULEDIR/modules.dep" ]; then
  modules_dep_file="$MODULEDIR/modules.dep"
fi

if [ -z "$modules_dep_file" ]; then
  modules_dep_file="/lib/modules/$(uname -r)/modules.dep"
fi

# TODO use "modprobe -nv $module" instead of module dependencies in variables
if [ -n "$discover_modules" ]; then
  echo discover_modules="\"$discover_modules\"" >> "$INITRDDIR/$CONFDIR/initrd-netboot.conf"
fi

for m in $nic_modules $nic_dep_modules $net_modules $discover_modules ; do
  module=$(egrep "/$m.o:|/$m.ko:" $modules_dep_file | cut -d : -f 1)
  if [ -n "$module" ]; then
    install_modules="$install_modules $module"
  fi
done
# copy found modules into initrd dir
for m in $install_modules ; do
  if [ -r "$m" ]; then
    mdir="$(dirname $m)"   
    mkdir -p "$INITRDDIR/$mdir"
    cp -a $m $INITRDDIR/$m
  fi
done
