#!/bin/sh

config() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "$(cat $OLD | md5sum)" = "$(cat $NEW | md5sum)" ]; then
    rm $NEW
  fi
}

config etc/vanguards/vanguards.conf.new

# ---------------------------------------------------------------------------
# Runtime directories, split per instance:
#
#   /var/lib/vanguards/system    system tor instance   (user tor)
#   /var/lib/vanguards/annode    tor-data instance     (user annode)
#   /var/log/vanguards/...       ditto
#   /var/run/vanguards/...       ditto  (tmpfs on Slackware 15+)
#
# Parents stay root:root 0755 so neither instance can write into the
# other's tree. The per-instance rc scripts chown only their own
# subdirectory; nothing chowns the shared parents.
# ---------------------------------------------------------------------------

# Parents -- corrects the old layout, where these were tor:tor 0750 and
# annode could not traverse them. mkdir + chown + chmod (not install -d)
# so an upgrade from the flat layout actually re-fixes the ownership
# instead of silently inheriting it.
mkdir -p var/lib/vanguards var/log/vanguards var/run/vanguards
chown root:root var/lib/vanguards var/log/vanguards var/run/vanguards \
  2>/dev/null || true
chmod 0755    var/lib/vanguards var/log/vanguards var/run/vanguards

# System instance (tor user -- assumed to already exist, per rc.tor).
mkdir -p var/lib/vanguards/system var/log/vanguards/system \
         var/run/vanguards/system
chown tor:tor var/lib/vanguards/system var/log/vanguards/system \
              var/run/vanguards/system 2>/dev/null || true
chmod 0750    var/lib/vanguards/system var/log/vanguards/system \
              var/run/vanguards/system

# Annode / tor-data instance. Only chown to annode if the user exists;
# otherwise create the dirs root-owned so that rc.vanguards-annode's
# own chown (runs as root on first start) can fix them once annode
# exists. Never leave the tree missing -- the rc script assumes the
# subdirectory exists or creates it, and either way must not fail.
if id annode >/dev/null 2>&1; then
  owner=annode; group=annode
else
  owner=root;   group=root
fi
mkdir -p var/lib/vanguards/annode var/log/vanguards/annode \
         var/run/vanguards/annode
chown "$owner:$group" var/lib/vanguards/annode \
                      var/log/vanguards/annode \
                      var/run/vanguards/annode 2>/dev/null || true
chmod 0750            var/lib/vanguards/annode \
                      var/log/vanguards/annode \
                      var/run/vanguards/annode

# ---------------------------------------------------------------------------
# One-time migration from the old flat layout.
#
# Old:   /var/lib/vanguards/vanguards.state   (tor)
#        /var/log/vanguards/vanguards.log     (tor)
# New:   /var/lib/vanguards/system/system.state
#        /var/log/vanguards/system/system.log
#
# Only move if the destination doesn't already exist, so an upgrade
# over a newer file won't clobber it. If both exist, leave the old
# file in place for the admin to reconcile by hand.
# ---------------------------------------------------------------------------

if [ -f var/lib/vanguards/vanguards.state ] && \
   [ ! -e var/lib/vanguards/system/system.state ]; then
  mv var/lib/vanguards/vanguards.state \
     var/lib/vanguards/system/system.state
  chown tor:tor var/lib/vanguards/system/system.state 2>/dev/null || true
fi

if [ -f var/log/vanguards/vanguards.log ] && \
   [ ! -e var/log/vanguards/system/system.log ]; then
  mv var/log/vanguards/vanguards.log \
     var/log/vanguards/system/system.log
  chown tor:tor var/log/vanguards/system/system.log 2>/dev/null || true
fi