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/node_modules ; rm -rf ep_etherpad-lite )
( cd var/etherpad-lite/node_modules ; ln -sf ../src ep_etherpad-lite )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf sshpk-verify )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../sshpk/bin/sshpk-verify sshpk-verify )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf esparse )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../esprima/bin/esparse.js esparse )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf mime )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../mime/cli.js mime )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf esvalidate )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../esprima/bin/esvalidate.js esvalidate )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf mocha )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../mocha/bin/mocha mocha )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf mkdirp )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../mkdirp/bin/cmd.js mkdirp )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf npx )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../npm/bin/npx-cli.js npx )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf acorn )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../acorn/bin/acorn acorn )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf sshpk-sign )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../sshpk/bin/sshpk-sign sshpk-sign )
( 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/.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 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 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 sshpk-conv )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../sshpk/bin/sshpk-conv sshpk-conv )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf esgenerate )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../escodegen/bin/esgenerate.js esgenerate )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf escodegen )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../escodegen/bin/escodegen.js escodegen )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf uuid )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../uuid/bin/uuid uuid )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf _mocha )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../mocha/bin/_mocha _mocha )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf follow )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../cloudant-follow/cli.js follow )
( cd var/etherpad-lite/src/node_modules/.bin ; rm -rf he )
( cd var/etherpad-lite/src/node_modules/.bin ; ln -sf ../he/bin/he he )
( 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/superagent/node_modules/.bin ; rm -rf mime )
( cd var/etherpad-lite/src/node_modules/superagent/node_modules/.bin ; ln -sf ../mime/cli.js mime )
( cd var/etherpad-lite/src/node_modules/pg/node_modules/.bin ; rm -rf semver )
( cd var/etherpad-lite/src/node_modules/pg/node_modules/.bin ; ln -sf ../semver/bin/semver semver )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf sshpk-verify )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../sshpk/bin/sshpk-verify sshpk-verify )
( 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 rc )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../rc/cli.js rc )
( 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/bin/opener-bin.js opener )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf mkdirp )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../mkdirp/bin/cmd.js mkdirp )
( 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 is-ci )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../is-ci/bin.js is-ci )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf JSONStream )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../JSONStream/bin.js JSONStream )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf sshpk-sign )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../sshpk/bin/sshpk-sign sshpk-sign )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf errno )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../errno/cli.js errno )
( 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 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 sshpk-conv )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../sshpk/bin/sshpk-conv sshpk-conv )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf uuid )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../uuid/bin/uuid uuid )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; rm -rf qrcode-terminal )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/.bin ; ln -sf ../qrcode-terminal/bin/qrcode-terminal.js qrcode-terminal )
( 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/node-gyp/node_modules/.bin ; rm -rf nopt )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/node-gyp/node_modules/.bin ; ln -sf ../nopt/bin/nopt.js nopt )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/node-gyp/node_modules/.bin ; rm -rf semver )
( cd var/etherpad-lite/src/node_modules/npm/node_modules/node-gyp/node_modules/.bin ; ln -sf ../semver/bin/semver semver )
( 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/wd/node_modules/.bin ; rm -rf har-validator )
( cd var/etherpad-lite/src/node_modules/wd/node_modules/.bin ; ln -sf ../har-validator/bin/har-validator har-validator )
