# Handle the incoming configuration files:
config() {
  for infile in $1; do
    NEW="$infile"
    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...
  done
}
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 permissions while moving into place:
preserve_perms etc/rc.d/rc.elogind.new
config etc/elogind/logind.conf.new
config etc/elogind/logind.conf.d/10-elogind.conf.new
config etc/elogind/sleep.conf.new
config etc/elogind/sleep.conf.d/10-elogind.conf.new
config etc/pam.d/elogind-user.new

# Reload elogind-daemon:
if pgrep -f elogind-daemon 1> /dev/null 2> /dev/null ; then
  pkill -HUP -f elogind-daemon
fi

( cd bin ; rm -rf elogind-inhibit )
( cd bin ; ln -sf /usr/bin/elogind-inhibit elogind-inhibit )
( cd bin ; rm -rf loginctl )
( cd bin ; ln -sf /usr/bin/loginctl loginctl )
( cd bin ; rm -rf userdbctl )
( cd bin ; ln -sf /usr/bin/userdbctl userdbctl )
( cd bin ; rm -rf varlinkctl )
( cd bin ; ln -sf /usr/bin/varlinkctl varlinkctl )
( cd etc/ssh/sshd_config.d ; rm -rf 20-elogind-userdb.conf )
( cd etc/ssh/sshd_config.d ; ln -sf ../../../lib64/elogind/sshd_config.d/20-elogind-userdb.conf 20-elogind-userdb.conf )
( cd lib64 ; rm -rf libelogind.so )
( cd lib64 ; ln -sf libelogind.so.0 libelogind.so )
( cd lib64 ; rm -rf libelogind.so.0 )
( cd lib64 ; ln -sf libelogind.so.0.40.0 libelogind.so.0 )
( cd usr/include/elogind ; rm -rf _sd-common.h )
( cd usr/include/elogind ; ln -sf systemd/_sd-common.h _sd-common.h )
( cd usr/include/elogind ; rm -rf sd-bus-protocol.h )
( cd usr/include/elogind ; ln -sf systemd/sd-bus-protocol.h sd-bus-protocol.h )
( cd usr/include/elogind ; rm -rf sd-bus-vtable.h )
( cd usr/include/elogind ; ln -sf systemd/sd-bus-vtable.h sd-bus-vtable.h )
( cd usr/include/elogind ; rm -rf sd-bus.h )
( cd usr/include/elogind ; ln -sf systemd/sd-bus.h sd-bus.h )
( cd usr/include/elogind ; rm -rf sd-daemon.h )
( cd usr/include/elogind ; ln -sf systemd/sd-daemon.h sd-daemon.h )
( cd usr/include/elogind ; rm -rf sd-device.h )
( cd usr/include/elogind ; ln -sf systemd/sd-device.h sd-device.h )
( cd usr/include/elogind ; rm -rf sd-event.h )
( cd usr/include/elogind ; ln -sf systemd/sd-event.h sd-event.h )
( cd usr/include/elogind ; rm -rf sd-gpt.h )
( cd usr/include/elogind ; ln -sf systemd/sd-gpt.h sd-gpt.h )
( cd usr/include/elogind ; rm -rf sd-hwdb.h )
( cd usr/include/elogind ; ln -sf systemd/sd-hwdb.h sd-hwdb.h )
( cd usr/include/elogind ; rm -rf sd-id128.h )
( cd usr/include/elogind ; ln -sf systemd/sd-id128.h sd-id128.h )
( cd usr/include/elogind ; rm -rf sd-journal.h )
( cd usr/include/elogind ; ln -sf systemd/sd-journal.h sd-journal.h )
( cd usr/include/elogind ; rm -rf sd-json.h )
( cd usr/include/elogind ; ln -sf systemd/sd-json.h sd-json.h )
( cd usr/include/elogind ; rm -rf sd-login.h )
( cd usr/include/elogind ; ln -sf systemd/sd-login.h sd-login.h )
( cd usr/include/elogind ; rm -rf sd-messages.h )
( cd usr/include/elogind ; ln -sf systemd/sd-messages.h sd-messages.h )
( cd usr/include/elogind ; rm -rf sd-path.h )
( cd usr/include/elogind ; ln -sf systemd/sd-path.h sd-path.h )
( cd usr/include/elogind ; rm -rf sd-varlink-idl.h )
( cd usr/include/elogind ; ln -sf systemd/sd-varlink-idl.h sd-varlink-idl.h )
( cd usr/include/elogind ; rm -rf sd-varlink.h )
( cd usr/include/elogind ; ln -sf systemd/sd-varlink.h sd-varlink.h )
