#!/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 [ "`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...
}

# Keep same perms on rc.turnserver.new:
if [ -e etc/rc.d/rc.turnserver ]; then
  cp -a etc/rc.d/rc.turnserver etc/rc.d/rc.turnserver.new.incoming
  cat etc/rc.d/rc.turnserver.new > etc/rc.d/rc.turnserver.new.incoming
  mv etc/rc.d/rc.turnserver.new.incoming etc/rc.d/rc.turnserver.new
fi

config etc/default/turnserver.new
config etc/turnserver/turnserver.conf.new
config etc/rc.d/rc.turnserver.new

# Create turnserver user / group
if chroot . getent group turnserver >/dev/null
then
  : turnserver group already present
else
  GROUPOPT="-g 366"
  if chroot . groupadd $GROUPOPT turnserver 2>/dev/null
  then
    : turnserver group successfully added
  else
    chroot . groupadd turnserver
  fi
fi

if chroot . getent passwd turnserver >/dev/null
then
  : turnserver user already present
else
  USEROPT="-g turnserver -u 366 -d /var/lib/turnserver"
  if chroot . useradd $USEROPT turnserver 2>/dev/null
  then
    : turnserver user successfully added
  else
    chroot . useradd turnserver
  fi
fi

