#!/bin/sh

FILES=""
CURDIR=`pwd`

if [ $# -lt 1 ] ; then
  echo "*** ERROR *** At least one filename must be given !!!"
  exit 0
fi

for file in $*
do
  aux=`grep $file  /etc/config_files`
  if [ x$file != x$aux ] ; then
    echo "*** ERROR *** $file not present in /etc/config_files !!!"
    exit 0
  fi
done

rm -rf /tmp/upd
mkdir /tmp/upd
chmod 0700 /tmp/upd
cd /tmp/upd

zcat /proc/flash/script 2>/dev/null | tar -px 2>/dev/null

st=$?

for file in $*
do
  if [ -f $file -o -d $file ] ; then
    if [ $st = 0 ] ; then
      my_dir=`dirname $file`
      install -d ./$my_dir
      install -p $file ./$file
    fi
    FILES="$FILES $file"
  else
    echo "$0 : File '$file' does not exist !"
  fi
done
echo -e "Updating configuration files: $FILES to Flash ... \c"
if [ $st = 0 ] ; then
  tar -pcf /config * > /dev/null 2>&1
else
  tar -pcf /config $FILES > /dev/null 2>&1
fi

if [ $? != 0 ]; then
    echo -e "FAILED!!\n"
    exit 1
fi
rm -rf /tmp/upd
cd /
gzip config
cp -p config.gz /proc/flash/script
rm -rf config.gz
cd $CURDIR
echo -e "done.\n"

