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

config etc/gitea/gitea.ini.new
config etc/logrotate.d/gitea.new
config etc/rc.d/rc.gitea.new

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

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

