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

BACKUP_FILE=(openrc/rc.conf logrotate.d/openrc)
BACKUP_CONF=(agetty bootmisc consolefont devfs dmesg fsck hostname hwclock keymaps killprocs localmount modules mtab net-online netmount network seedrng staticroute swap swclock)

for file in "${BACKUP_FILE[@]}"; do
  config "etc/${file}.new"
done

for file in "${BACKUP_CONF[@]}"; do
  config "etc/openrc/conf.d/${file}.new"
done

# enable cgroups service as required by openrc 0.35+
[ ! -e etc/openrc/runlevels/sysinit/cgroups ] && ln -s /etc/openrc/init.d/cgroups etc/openrc/runlevels/sysinit/cgroups

# enable save keymaps and termencoding services as needed by openrc 0.40+
[ ! -e etc/openrc/runlevels/boot/save-keymaps ] && ln -s /etc/openrc/init.d/save-keymaps etc/openrc/runlevels/boot/save-keymaps
[ ! -e etc/openrc/runlevels/boot/save-termencoding ] && ln -s /etc/openrc/init.d/save-termencoding etc/openrc/runlevels/boot/save-termencoding
( cd etc/openrc/init.d ; rm -rf functions.sh )
( cd etc/openrc/init.d ; ln -sf /lib/rc/sh/functions.sh functions.sh )
( cd etc/openrc/runlevels/boot ; rm -rf binfmt )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/binfmt binfmt )
( cd etc/openrc/runlevels/boot ; rm -rf bootmisc )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/bootmisc bootmisc )
( cd etc/openrc/runlevels/boot ; rm -rf fsck )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/fsck fsck )
( cd etc/openrc/runlevels/boot ; rm -rf hostname )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/hostname hostname )
( cd etc/openrc/runlevels/boot ; rm -rf hwclock )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/hwclock hwclock )
( cd etc/openrc/runlevels/boot ; rm -rf keymaps )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/keymaps keymaps )
( cd etc/openrc/runlevels/boot ; rm -rf localmount )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/localmount localmount )
( cd etc/openrc/runlevels/boot ; rm -rf loopback )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/loopback loopback )
( cd etc/openrc/runlevels/boot ; rm -rf modules )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/modules modules )
( cd etc/openrc/runlevels/boot ; rm -rf mtab )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/mtab mtab )
( cd etc/openrc/runlevels/boot ; rm -rf network )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/network network )
( cd etc/openrc/runlevels/boot ; rm -rf procfs )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/procfs procfs )
( cd etc/openrc/runlevels/boot ; rm -rf root )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/root root )
( cd etc/openrc/runlevels/boot ; rm -rf save-keymaps )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/save-keymaps save-keymaps )
( cd etc/openrc/runlevels/boot ; rm -rf save-termencoding )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/save-termencoding save-termencoding )
( cd etc/openrc/runlevels/boot ; rm -rf seedrng )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/seedrng seedrng )
( cd etc/openrc/runlevels/boot ; rm -rf staticroute )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/staticroute staticroute )
( cd etc/openrc/runlevels/boot ; rm -rf swap )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/swap swap )
( cd etc/openrc/runlevels/boot ; rm -rf sysctl )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/sysctl sysctl )
( cd etc/openrc/runlevels/boot ; rm -rf termencoding )
( cd etc/openrc/runlevels/boot ; ln -sf /etc/openrc/init.d/termencoding termencoding )
( cd etc/openrc/runlevels/default ; rm -rf local )
( cd etc/openrc/runlevels/default ; ln -sf /etc/openrc/init.d/local local )
( cd etc/openrc/runlevels/default ; rm -rf netmount )
( cd etc/openrc/runlevels/default ; ln -sf /etc/openrc/init.d/netmount netmount )
( cd etc/openrc/runlevels/nonetwork ; rm -rf local )
( cd etc/openrc/runlevels/nonetwork ; ln -sf /etc/openrc/init.d/local local )
( cd etc/openrc/runlevels/shutdown ; rm -rf killprocs )
( cd etc/openrc/runlevels/shutdown ; ln -sf /etc/openrc/init.d/killprocs killprocs )
( cd etc/openrc/runlevels/shutdown ; rm -rf mount-ro )
( cd etc/openrc/runlevels/shutdown ; ln -sf /etc/openrc/init.d/mount-ro mount-ro )
( cd etc/openrc/runlevels/shutdown ; rm -rf savecache )
( cd etc/openrc/runlevels/shutdown ; ln -sf /etc/openrc/init.d/savecache savecache )
( cd etc/openrc/runlevels/sysinit ; rm -rf cgroups )
( cd etc/openrc/runlevels/sysinit ; ln -sf /etc/openrc/init.d/cgroups cgroups )
( cd etc/openrc/runlevels/sysinit ; rm -rf devfs )
( cd etc/openrc/runlevels/sysinit ; ln -sf /etc/openrc/init.d/devfs devfs )
( cd etc/openrc/runlevels/sysinit ; rm -rf dmesg )
( cd etc/openrc/runlevels/sysinit ; ln -sf /etc/openrc/init.d/dmesg dmesg )
( cd etc/openrc/runlevels/sysinit ; rm -rf sysfs )
( cd etc/openrc/runlevels/sysinit ; ln -sf /etc/openrc/init.d/sysfs sysfs )
( cd lib64 ; rm -rf libeinfo.so )
( cd lib64 ; ln -sf libeinfo.so.1 libeinfo.so )
( cd lib64 ; rm -rf librc.so )
( cd lib64 ; ln -sf librc.so.1 librc.so )
( cd usr/man/man3 ; rm -rf ebegin.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ebegin.3.gz )
( cd usr/man/man3 ; rm -rf ebeginn.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ebeginn.3.gz )
( cd usr/man/man3 ; rm -rf ebeginv.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ebeginv.3.gz )
( cd usr/man/man3 ; rm -rf ebeginvn.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ebeginvn.3.gz )
( cd usr/man/man3 ; rm -rf ebracket.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ebracket.3.gz )
( cd usr/man/man3 ; rm -rf eend.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eend.3.gz )
( cd usr/man/man3 ; rm -rf eendv.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eendv.3.gz )
( cd usr/man/man3 ; rm -rf eerror.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eerror.3.gz )
( cd usr/man/man3 ; rm -rf eerrorn.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eerrorn.3.gz )
( cd usr/man/man3 ; rm -rf eerrorx.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eerrorx.3.gz )
( cd usr/man/man3 ; rm -rf eindent.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eindent.3.gz )
( cd usr/man/man3 ; rm -rf eindentv.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eindentv.3.gz )
( cd usr/man/man3 ; rm -rf einfon.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz einfon.3.gz )
( cd usr/man/man3 ; rm -rf einfov.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz einfov.3.gz )
( cd usr/man/man3 ; rm -rf einfovn.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz einfovn.3.gz )
( cd usr/man/man3 ; rm -rf eoutdent.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eoutdent.3.gz )
( cd usr/man/man3 ; rm -rf eoutdentv.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eoutdentv.3.gz )
( cd usr/man/man3 ; rm -rf eprefix.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz eprefix.3.gz )
( cd usr/man/man3 ; rm -rf ewarn.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ewarn.3.gz )
( cd usr/man/man3 ; rm -rf ewarnn.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ewarnn.3.gz )
( cd usr/man/man3 ; rm -rf ewarnv.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ewarnv.3.gz )
( cd usr/man/man3 ; rm -rf ewarnvn.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ewarnvn.3.gz )
( cd usr/man/man3 ; rm -rf ewarnx.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ewarnx.3.gz )
( cd usr/man/man3 ; rm -rf ewend.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ewend.3.gz )
( cd usr/man/man3 ; rm -rf ewendv.3.gz )
( cd usr/man/man3 ; ln -sf einfo.3.gz ewendv.3.gz )
( cd usr/man/man3 ; rm -rf rc_config_list.3.gz )
( cd usr/man/man3 ; ln -sf rc_config.3.gz rc_config_list.3.gz )
( cd usr/man/man3 ; rm -rf rc_config_load.3.gz )
( cd usr/man/man3 ; ln -sf rc_config.3.gz rc_config_load.3.gz )
( cd usr/man/man3 ; rm -rf rc_config_value.3.gz )
( cd usr/man/man3 ; ln -sf rc_config.3.gz rc_config_value.3.gz )
( cd usr/man/man3 ; rm -rf rc_deptree_depend.3.gz )
( cd usr/man/man3 ; ln -sf rc_deptree.3.gz rc_deptree_depend.3.gz )
( cd usr/man/man3 ; rm -rf rc_deptree_depends.3.gz )
( cd usr/man/man3 ; ln -sf rc_deptree.3.gz rc_deptree_depends.3.gz )
( cd usr/man/man3 ; rm -rf rc_deptree_free.3.gz )
( cd usr/man/man3 ; ln -sf rc_deptree.3.gz rc_deptree_free.3.gz )
( cd usr/man/man3 ; rm -rf rc_deptree_load.3.gz )
( cd usr/man/man3 ; ln -sf rc_deptree.3.gz rc_deptree_load.3.gz )
( cd usr/man/man3 ; rm -rf rc_deptree_order.3.gz )
( cd usr/man/man3 ; ln -sf rc_deptree.3.gz rc_deptree_order.3.gz )
( cd usr/man/man3 ; rm -rf rc_deptree_update.3.gz )
( cd usr/man/man3 ; ln -sf rc_deptree.3.gz rc_deptree_update.3.gz )
( cd usr/man/man3 ; rm -rf rc_deptree_update_needed.3.gz )
( cd usr/man/man3 ; ln -sf rc_deptree.3.gz rc_deptree_update_needed.3.gz )
( cd usr/man/man3 ; rm -rf rc_runlevel_exists.3.gz )
( cd usr/man/man3 ; ln -sf rc_runlevel.3.gz rc_runlevel_exists.3.gz )
( cd usr/man/man3 ; rm -rf rc_runlevel_get.3.gz )
( cd usr/man/man3 ; ln -sf rc_runlevel.3.gz rc_runlevel_get.3.gz )
( cd usr/man/man3 ; rm -rf rc_runlevel_list.3.gz )
( cd usr/man/man3 ; ln -sf rc_runlevel.3.gz rc_runlevel_list.3.gz )
( cd usr/man/man3 ; rm -rf rc_runlevel_set.3.gz )
( cd usr/man/man3 ; ln -sf rc_runlevel.3.gz rc_runlevel_set.3.gz )
( cd usr/man/man3 ; rm -rf rc_runlevel_starting.3.gz )
( cd usr/man/man3 ; ln -sf rc_runlevel.3.gz rc_runlevel_starting.3.gz )
( cd usr/man/man3 ; rm -rf rc_runlevel_stopping.3.gz )
( cd usr/man/man3 ; ln -sf rc_runlevel.3.gz rc_runlevel_stopping.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_add.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_add.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_daemon_set.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_daemon_set.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_daemons_crashed.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_daemons_crashed.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_delete.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_delete.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_description.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_description.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_exists.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_exists.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_extra_commands.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_extra_commands.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_in_runlevel.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_in_runlevel.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_mark.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_mark.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_plugable.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_plugable.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_resolve.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_resolve.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_schedule_clear.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_schedule_clear.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_schedule_start.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_schedule_start.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_started_daemon.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_started_daemon.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_state.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_state.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_value_get.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_value_get.3.gz )
( cd usr/man/man3 ; rm -rf rc_service_value_set.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_service_value_set.3.gz )
( cd usr/man/man3 ; rm -rf rc_services_in_runlevel.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_services_in_runlevel.3.gz )
( cd usr/man/man3 ; rm -rf rc_services_in_state.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_services_in_state.3.gz )
( cd usr/man/man3 ; rm -rf rc_services_scheduled.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_services_scheduled.3.gz )
( cd usr/man/man3 ; rm -rf rc_services_scheduled_by.3.gz )
( cd usr/man/man3 ; ln -sf rc_service.3.gz rc_services_scheduled_by.3.gz )
( cd usr/man/man3 ; rm -rf rc_stringlist_add.3.gz )
( cd usr/man/man3 ; ln -sf rc_stringlist.3.gz rc_stringlist_add.3.gz )
( cd usr/man/man3 ; rm -rf rc_stringlist_addu.3.gz )
( cd usr/man/man3 ; ln -sf rc_stringlist.3.gz rc_stringlist_addu.3.gz )
( cd usr/man/man3 ; rm -rf rc_stringlist_delete.3.gz )
( cd usr/man/man3 ; ln -sf rc_stringlist.3.gz rc_stringlist_delete.3.gz )
( cd usr/man/man3 ; rm -rf rc_stringlist_free.3.gz )
( cd usr/man/man3 ; ln -sf rc_stringlist.3.gz rc_stringlist_free.3.gz )
( cd usr/man/man3 ; rm -rf rc_stringlist_new.3.gz )
( cd usr/man/man3 ; ln -sf rc_stringlist.3.gz rc_stringlist_new.3.gz )
( cd usr/man/man3 ; rm -rf rc_stringlist_sort.3.gz )
( cd usr/man/man3 ; ln -sf rc_stringlist.3.gz rc_stringlist_sort.3.gz )
( cd usr/man/man3 ; rm -rf rc_yesno.3.gz )
( cd usr/man/man3 ; ln -sf rc_config.3.gz rc_yesno.3.gz )
