#!/bin/sh -e

#DEBHELPER#

# Breakage: <= 2.4.1 used jwhois.db.db, 2.4.2+ use jwhois.db
# Breakage: 2.4.2-1 did not recognize this (so if we're upgrading
# from it, we might get doubled caches)

if test "$1" = "configure"; then
  if dpkg --compare-versions "$2" le "2.4.2-1"; then
    # If old cache exists, and new does not, move it
    if test -e /var/cache/jwhois/jwhois.db.db -a ! -e /var/cache/jwhois/jwhois.db; then
      mv -f /var/cache/jwhois/jwhois.db.db /var/cache/jwhois/jwhois.db
    fi
    # Remove possible left-over old cache
    rm -f /var/cache/jwhois/jwhois.db.db
  fi
fi

# Add the group 'jwhois' that the cache uses

groupexist=1
getent group jwhois > /dev/null || groupexist=0
if test "$groupexist" = "0"; then
  # Group did not exist
  addgroup --system jwhois || echo "Failed to create jwhois group, cache functionality may be impaired."
fi

# Setgid binary to the jwhois group if no statoverride was set

jwhoisoverride=1
dpkg-statoverride --list /usr/bin/jwhois > /dev/null || jwhoisoverride=0

if test "$jwhoisoverride" = "0"; then
  if getent group jwhois > /dev/null; then
    if chown root.jwhois /usr/bin/jwhois; then
      chmod 02755 /usr/bin/jwhois || echo "Failed setting jwhois permissions, cache functionality may be impaired."
    else
      echo "Failed setting jwhois ownership, cache functionality may be impaired."
    fi
  else
    echo "Jwhois group is missing, cache functionality will be impaired."
  fi
fi

# Setgid cache directory to the jwhois group unless it's overriden

cacheoverride=1
dpkg-statoverride --list /var/cache/jwhois > /dev/null || cacheoverride=0

if test "$cacheoverride" = "0"; then
  if getent group jwhois > /dev/null; then
    if chown root.jwhois /var/cache/jwhois; then
      chmod 0775 /var/cache/jwhois || echo "Failed setting cache directory permissions, cache functionality may be impaired."
    else
      echo "Failed setting cache directory ownership, cache functionality may be impaired."
    fi

    # Make sure the cache, if it exists, has the correct ownership,
    # and if it doesn't exist, create an empty one so that it is owned
    # by root (bug#95013).

    if test -e /var/cache/jwhois/jwhois.db; then
      chown root.jwhois /var/cache/jwhois/jwhois.db || echo "Failed setting cache file ownership, cache functionality may be impaired."
      chmod 0660 /var/cache/jwhois/jwhois.db || echo "Failed setting cache permissions, cache functionality may be impaired."
    else
      if touch /var/cache/jwhois/jwhois.db; then
        if test -f /var/cache/jwhois/jwhois.db; then
          chown root.jwhois /var/cache/jwhois/jwhois.db || echo "Failed setting cache file ownership, cache functionality may be impaired."
          chmod 0660 /var/cache/jwhois/jwhois.db || echo "Failed setting cache permissions, cache functionality may be impaired."
        else
          echo "Cache file is not a regular file, cache functionality may be impaired."
        fi
      else
        echo "Failed creating cache file, cache functionality may be impaired."
      fi
    fi
  else
    echo " Jwhois group is missing, not setting ownership of cache."
  fi
fi
