#!/bin/sh
config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
     mv $NEW $OLD
  elif [ "`basename $NEW`" == "rc.local.new" ] && [ "`grep startx $OLD`" == "" ] ; then
     echo "# SlaXBMC start-up variables" >> $OLD
     echo "# If you do not wish this to be executed then comment it out." >> $OLD
     echo "sh /etc/rc.d/rc.wicd \"start\" &" >> $OLD
     echo "sh /etc/rc.d/rc.lirc \"start\" &" >> $OLD
     echo "sh /etc/rc.d/rc.dated \"start\" &" >> $OLD
     echo "sh /etc/rc.d/rc.avahidaemon \"start\" &" >> $OLD
     echo "/usr/sbin/sntp -s pool.ntp.org 2>&1 >/dev/null &" >> $OLD
     echo "su - root -c startx &" >> $OLD
     echo "" >> $OLD
  elif [ "`basename $NEW`" == "default.pa.new" ] && [ "`grep combined $OLD`" == "" ] ; then
     echo "# SlaXBMC start-up variables" >> $OLD
     echo "# If you do not wish this to be executed then comment it out." >> $OLD
     echo "load-module module-combine-sink sink_name=combined" >> $OLD
     echo "set-default-sink combined" >> $OLD
     echo "" >> $OLD
  elif [ "`basename $NEW`" == "e2fsck.conf.new" ] && [ "`grep broken_system_clock $OLD`" == "" ] ; then
     echo "[options]" >> $OLD
     echo "  accept_time_fudge = 1" >> $OLD
     echo "  broken_system_clock = 1" >> $OLD
     echo "" >> $OLD
  elif [ "`basename $NEW`" == "sysctl.conf.new" ] && [ "`grep min_free_kbytes $OLD`" == "" ] ; then
     echo "vm.min_free_kbytes = 32768" >> $OLD
     echo "vm.swappiness = 1" >> $OLD
     echo "vm.vfs_cache_pressure = 10" >> $OLD
     echo "" >> $OLD
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
     rm $NEW
  fi
# Otherwise, we leave the .new copy for the admin to consider...
}

# Remove files that might exist in /tmp matching out filenames!
if [ -f /tmp/default.pa.new ] ; then
   rm -f /tmp/default.pa.new
fi

if [ -f /tmp/rc.local.new ] ; then
   rm -f /tmp/rc.local.new
fi

if [ -f /tmp/rc.dated.new ] ; then
   rm -f /tmp/rc.dated.new
fi

if [ -f /tmp/smb.conf.new ] ; then
   rm -f /tmp/smb.conf.new
fi

if [ -f /tmp/e2fsck.conf.new ] ; then
   rm -f /tmp/e2fsck.conf.new
fi

if [ -f /tmp/sysctl.conf.new ] ; then
   rm -f /tmp/sysctl.conf.new
fi

if [ -f /tmp/alsa-base.conf.new ] ; then
   rm -f /tmp/alsa-base.conf.new
fi

if [ -f /tmp/.asoundrc.new ] ; then
   rm -f /tmp/.asoundrc.new
fi

if [ -f /tmp/.profile.new ] ; then
   rm -f /tmp/.profile.new
fi

if [ -f /tmp/.xinitrc.new ] ; then
   rm -f /tmp/.xinitrc.new
fi

if [ ! -d /var/run/dated ] ; then
   mkdir /var/run/dated
fi

config etc/pulse/default.pa.new
config etc/rc.d/rc.local.new
config etc/rc.d/rc.dated.new
config etc/samba/smb.conf.new
config etc/e2fsck.conf.new
config etc/sysctl.conf.new
config lib/modprobe.d/alsa-base.conf.new
config root/.asoundrc.new
config root/.profile.new
config root/.xinitrc.new
config root/.gtkrc-2.0.new
config boot/slaxbmc.bmp.new

if [ -f /mnt/etc/passwd ] && [ -f /mnt/etc/shadow ] && [ -f /mnt/etc/group ] ; then
   echo "avahi:x:501:" >> /mnt/etc/group
   echo "pulse:x:502:" >> /mnt/etc/group
   echo "avahi:!:15605:0:99999:7:::" >> /mnt/etc/shadow
   echo "pulse:!:15605:0:99999:7:::" >> /mnt/etc/shadow
   echo "avahi:x:501:501:Avahi Daemon:/dev/null:/bin/false" >> /mnt/etc/passwd
   echo "pulse:x:502:502:PulseAudio:/dev/null:/bin/false" >> /mnt/etc/passwd
elif [ -f /etc/passwd ] && [ -f /etc/shadow ] && [ -f /etc/group ] ; then
   if [ "`grep avahi /etc/passwd`" == "" ] && [ "`grep avahi /etc/shadow`" == "" ] && [ "`grep avahi /etc/group`" == "" ] ; then
      echo "avahi:x:501:" >> /etc/group
      echo "avahi:!:15605:0:99999:7:::" >> /etc/shadow
      echo "avahi:x:501:501:Avahi Daemon:/dev/null:/bin/false" >> /etc/passwd
   fi
   if [ "`grep pulse /etc/passwd`" == "" ] && [ "`grep pulse /etc/shadow`" == "" ] && [ "`grep pulse /etc/group`" == "" ] ; then
      echo "pulse:x:502:" >> /etc/group
      echo "pulse:!:15605:0:99999:7:::" >> /etc/shadow
      echo "pulse:x:502:502:PulseAudio:/dev/null:/bin/false" >> /etc/passwd
   fi
fi

if [ -f /mnt/etc/inittab ] && [ "`grep '#s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100' /mnt/etc/inittab`" != "" ] ; then
     sed -i 's!#s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100!#s0:12345:respawn:/sbin/agetty -L ttyAMA0 9600 vt100!g' /mnt/etc/inittab
elif [ -f /mnt/etc/inittab ] && [ "`grep 's0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100' /mnt/etc/inittab`" != "" ] ; then
     sed -i 's!s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100!#s0:12345:respawn:/sbin/agetty -L ttyAMA0 9600 vt100!g' /mnt/etc/inittab
elif [ -f /etc/inittab ] && [ "`grep '#s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100' /etc/inittab`" != "" ] ; then
     sed -i 's!#s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100!#s0:12345:respawn:/sbin/agetty -L ttyAMA0 9600 vt100!g' /etc/inittab
elif [ -f /etc/inittab ] && [ "`grep 's0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100' /etc/inittab`" != "" ] ; then
     sed -i 's!s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100!#s0:12345:respawn:/sbin/agetty -L ttyAMA0 9600 vt100!g' /etc/inittab
fi

if [ -f /mnt/etc/udev/rules.d/70-persistent-net.rules ] && [ "`grep SUBSYSTEM /mnt/etc/udev/rules.d/70-persistent-net.rules`" != "" ] ; then
     echo "#" > /mnt/etc/udev/rules.d/70-persistent-net.rules
elif [ -f /etc/udev/rules.d/70-persistent-net.rules ] && [ "`grep SUBSYSTEM /etc/udev/rules.d/70-persistent-net.rules`" != "" ] ; then
     echo "#" > /etc/udev/rules.d/70-persistent-net.rules
fi
