#!/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.nginx.new:
if [ -e etc/rc.d/rc.nginx ]; then
  cp -a etc/rc.d/rc.nginx etc/rc.d/rc.nginx.new.incoming
  cat etc/rc.d/rc.nginx.new > etc/rc.d/rc.nginx.new.incoming
  mv etc/rc.d/rc.nginx.new.incoming etc/rc.d/rc.nginx.new
fi

config etc/nginx/fastcgi.conf.new
config etc/nginx/fastcgi_params.new
config etc/nginx/koi-utf.new
config etc/nginx/koi-win.new
config etc/nginx/mime.types.new
config etc/nginx/nginx.conf.new
config etc/nginx/scgi_params.new
config etc/nginx/uwsgi_params.new
config etc/nginx/win-utf.new
config etc/nginx/conf.d/default.conf.new
config etc/rc.d/rc.nginx.new

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

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

( cd etc/nginx ; rm -rf modules )
( cd etc/nginx ; ln -sf /usr/lib64/nginx/modules modules )
