#!/bin/sh

###################################
# Version number of Simple Features
###################################
version="1.0rc2"

# Save version reference
echo "SF: $version" > /etc/simpleFeatures-version

cfg="/boot/config/simpleFeatures.cfg"
ini="/var/local/emhttp/simpleFeatures.ini"

# Confirmation settings
section="confirm"
echo "[$section]" >> $ini
before=$(du -b $ini | cut -f1)
if [ -f $cfg ]; then
# Create custom settings
  grep "^$section\." $cfg | sed "s:^$section\.::" >> $ini
fi
after=$(du -b $ini | cut -f1)
if [ $before -eq $after ]; then
# Create default settings
  echo 'down="1"' >> $ini
  echo 'stop="1"' >> $ini
  echo 'sleep="1"'>> $ini
  echo 'warn="1"' >> $ini
fi

# Display settings
section="display"
echo "[$section]" >> $ini
before=$(du -b $ini | cut -f1)
if [ -f $cfg ]; then
# Create custom settings
  grep "^$section\." $cfg | sed "s:^$section\.::" >> $ini
fi
after=$(du -b $ini | cut -f1)
if [ $before -eq $after ]; then
# Create default settings
  echo 'date="%c"'     >> $ini
  echo 'time="%R"'     >> $ini
  echo 'number=".,"'   >> $ini
  echo 'unit="C"'      >> $ini
  echo 'scale="-1"'    >> $ini
  echo 'align="right"' >> $ini
  echo 'view=""'       >> $ini
  echo 'total="1"'     >> $ini
  echo 'spin="1"'      >> $ini
  echo 'size="1"'      >> $ini
  echo 'snow="0"'      >> $ini
  echo 'button="1"'    >> $ini
  echo 'icons=""'      >> $ini
  echo 'banner=""'     >> $ini
  echo 'refresh="0"'   >> $ini
fi

# Parity Check settings
section="parity"
echo "[$section]" >> $ini
before=$(du -b $ini | cut -f1)
if [ -f $cfg ]; then
# Create custom settings
  grep "^$section\." $cfg | sed "s:^$section\.::" >> $ini
  entry=$(grep 'cron=' $ini | cut -f2 -d"'")
  if [ "$entry" != "" ]; then
# Create cron entry
    cron=/tmp/parity.tmp
    crontab -l | grep -v 'mdcmd check' | grep -v 'Parity Check' > $cron
    echo '# Scheduled Parity Check' >> $cron
    echo "$entry" >> $cron
    crontab $cron
    rm $cron
  fi
fi
after=$(du -b $ini | cut -f1)
if [ $before -eq $after ]; then
# Create default settings
  echo 'mode="0"'   >> $ini
  echo 'hour="0 0"' >> $ini
  echo 'dotm="1"'   >> $ini
  echo 'month="1"'  >> $ini
  echo 'day="0"'    >> $ini
  echo 'cron=""'    >> $ini
  echo 'write=""'   >> $ini
fi

# Notification settings
section="notify"
echo "[$section]" >> $ini
before=$(du -b $ini | cut -f1)
if [ -f $cfg ]; then
# Create custom settings
  grep "^$section\." $cfg | sed "s:^$section\.::" >> $ini
fi
after=$(du -b $ini | cut -f1)
if [ $before -eq $after ]; then
# Create default settings
  echo 'date="d/m/Y"' >> $ini
  echo 'time="h:i A"' >> $ini
  echo 'path="/boot/config/plugins/Notifications"' >> $ini
  echo 'position="top-right"' >> $ini
fi

# Remove legacy configuration file
rm -f /boot/config/ultra.cfg

echo "****************************************************************"
echo "* simpleFeatures webGUI has been installed"
echo "*"
echo "* Created by Andrew Hamer-Adams"
echo "* http://www.pixeleyes.co.nz"
echo "*"
echo "* Looks best on modern browsers"
echo "* Includes revamped graphical interface"
echo "* Includes modifications made by Bergware"
echo "* Version: $version"
echo "****************************************************************"
