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

config etc/valkey/valkey.conf.new
config etc/valkey/sentinel.conf.new
config etc/logrotate.d/valkey.new
config etc/rc.d/rc.valkey.new

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

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

( cd usr/bin ; rm -rf redis-benchmark )
( cd usr/bin ; ln -sf valkey-benchmark redis-benchmark )
( cd usr/bin ; rm -rf redis-check-aof )
( cd usr/bin ; ln -sf valkey-check-aof redis-check-aof )
( cd usr/bin ; rm -rf redis-check-rdb )
( cd usr/bin ; ln -sf valkey-check-rdb redis-check-rdb )
( cd usr/bin ; rm -rf redis-cli )
( cd usr/bin ; ln -sf valkey-cli redis-cli )
( cd usr/bin ; rm -rf redis-sentinel )
( cd usr/bin ; ln -sf valkey-sentinel redis-sentinel )
( cd usr/bin ; rm -rf redis-server )
( cd usr/bin ; ln -sf valkey-server redis-server )
( cd usr/bin ; rm -rf valkey-check-aof )
( cd usr/bin ; ln -sf valkey-server valkey-check-aof )
( cd usr/bin ; rm -rf valkey-check-rdb )
( cd usr/bin ; ln -sf valkey-server valkey-check-rdb )
( cd usr/bin ; rm -rf valkey-sentinel )
( cd usr/bin ; ln -sf valkey-server valkey-sentinel )
