#!/bin/bash
source /etc/nfsroot/def
if test $# = "0"; then
  echo 'nr2etc: copy files from /tftpboot/$IP to /etc'
  echo "usage: nr2etc x.x.x.x"
  echo "with x.x.x.x an existing IP number in /tftpboot"
  exit
fi

rm -rf $TMPDIR
mkdir $TMPDIR

for ip in $*; do
  if test -d /tftpboot/$ip; then
    cd /tftpboot
    if test -f /etc/nfsroot/ignore2etc.$ip; then
      ignore=/etc/nfsroot/ignore2etc.$ip;
    else
      ignore=/etc/nfsroot/ignore2etc
    fi
    cat $ignore |grep -v "^$"|grep -v "^#"|sed -e "s/\//$ip\//"> $TMPDIR/ignore2etc
    ignore=$TMPDIR/ignore2etc
    FILES=`cd /tftboot/; find $ip -mindepth 1 -not -type d -a -not -type l|egrep -v -f $ignore`
    FILES="$FILES "`cd /tftpboot; find $ip -mindepth 1 -type d -empty|egrep -v -f $ignore`
    (cd /tftpboot/; tar -cf - $FILES)|(cd /etc/nfsroot; tar -xf -)
    echo removing directory /tftpboot/$ip
    echo rm /tftpboot/$ip
    echo recreating /tmp/tftpboot/$ip
    echo mknfsroot $ip
  fi
done


