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...
}

preserve_perms() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  if [ -e $OLD ]; then
    cp -a $OLD ${NEW}.incoming
    cat $NEW > ${NEW}.incoming
    mv ${NEW}.incoming $NEW
  fi
  config $NEW
}

preserve_perms etc/rc.d/rc.etherpad-lite.new
config etc/etherpad-lite/settings.json.new

touch var/log/$PRGNAM/etherpad.log
touch var/log/$PRGNAM/error.log

( cd srv ; rm -rf etherpad-lite )
( cd srv ; ln -sf /var/etherpad-lite etherpad-lite )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf wd )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../wd/lib/bin.js wd )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf npm )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../npm/bin/npm-cli.js npm )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf uglifyjs )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../uglify-js/bin/uglifyjs uglifyjs )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf express )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../express/bin/express express )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf semver )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../semver/bin/semver semver )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf nodemailer )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../nodemailer/bin/nodemailer nodemailer )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf cleancss )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../clean-css/bin/cleancss cleancss )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf semver )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../semver/bin/semver semver )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf node-gyp )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../node-gyp/bin/node-gyp.js node-gyp )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf nopt )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../nopt/bin/nopt.js nopt )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf which )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../which/bin/which which )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf rimraf )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../rimraf/bin.js rimraf )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf opener )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../opener/opener.js opener )
( cd var/etherpad-lite/src/node_modules/nodemailer/node_modules/mailcomposer/node_modules/.bin ; rm -rf he )
( cd var/etherpad-lite/src/node_modules/nodemailer/node_modules/mailcomposer/node_modules/.bin ; ln -sf ../he/bin/he he )
( cd var/etherpad-lite/src/node_modules/swagger-node-express/node_modules/.bin ; rm -rf docco )
( cd var/etherpad-lite/src/node_modules/swagger-node-express/node_modules/.bin ; ln -sf ../docco/bin/docco docco )
( cd var/etherpad-lite/src/node_modules/socket.io/node_modules/socket.io-client/node_modules/.bin ; rm -rf wscat )
( cd var/etherpad-lite/src/node_modules/socket.io/node_modules/socket.io-client/node_modules/.bin ; ln -sf ../ws/bin/wscat wscat )
( cd var/etherpad-lite/src/node_modules/log4js/node_modules/.bin ; rm -rf semver )
( cd var/etherpad-lite/src/node_modules/log4js/node_modules/.bin ; ln -sf ../semver/bin/semver semver )
( cd var/etherpad-lite/src/node_modules/ueberDB/node_modules/pg/node_modules/.bin ; rm -rf semver )
( cd var/etherpad-lite/src/node_modules/ueberDB/node_modules/pg/node_modules/.bin ; ln -sf ../semver/bin/semver semver )
( cd var/etherpad-lite/node_modules ; rm -rf ep_etherpad-lite )
( cd var/etherpad-lite/node_modules ; ln -sf ../src ep_etherpad-lite )
