#!/bin/sh

# This script converts all db files of a cyrus installation from their
# existing format to the format required by the current installation.
# The format of current db files is determined using the 'file' command
# with a magic file added for skiplist db, the new format is read from
# a config file usually in /usr/share/cyrus-imapd/rpm/db.cfg, which is
# created while compiling. After converting, the db.cfg file is
# copied to a cache file usually at /var/lib/imap/rpm/db.cfg.cache to
# allow bypassing this converting script if both files are identical.
# While this is a bit less secure, it may be useful on big server where
# db converting is done automatically.
#
# This script can safely be run as root, it will reexec itself as user
# cyrus if needed.
#
# author: Simon Matter, Invoca Systems <simon.matter@invoca.ch>

# changelog
# v1.0.1, Oct 22 2002 Simon Matter <simon.matter@invoca.ch>
# - added two-step conversion method
#
# v1.0.2, Jan 10 2003 Simon Matter <simon.matter@invoca.ch>
# - fixed a bug where cvt_cyrusdb was called to convert empty or
#   nonexistent files
#
# v1.0.2, Mar 14 2003 Simon Matter <simon.matter@invoca.ch>
# - fixed a problem with new versions of the file command

if [ -n "`/sbin/pidof cyrus-master`" ]; then
  echo "ERROR: cyrus-master is running, unable to convert mailboxes!"
  exit 1
fi

# force cyrus user for security reasons
if [ ! $(whoami) = "cyrus" ]; then
  exec su - cyrus -c "cd $PWD ; $0"
fi

db_cfg=/usr/share/cyrus-imapd/rpm/db.cfg
cyrus_magic=/usr/share/cyrus-imapd/rpm/magic
cvt_cyrusdb=/usr/lib/cyrus/cvt_cyrusdb
imap_prefix=/var/lib/imap

# files get mode 0600
umask 166

# source compiled db backend config
. $db_cfg

# file_type [file]
file_type() {
  this_type=$(file -b -m "$cyrus_magic":/usr/share/magic "$1" 2> /dev/null)
  if echo "$this_type" | grep -qi skip > /dev/null 2>&1; then
    echo skiplist
  elif echo "$this_type" | grep -qi text > /dev/null 2>&1; then
    echo flat
  else
    echo db3
  fi
}

# cvt_file [file] [db]
cvt_file() {
  target="$1"
  new_db="$2"
  if [ -s "$target" ]; then
    old_db=$(file_type "$target")
    if [ ! "$old_db" = "$new_db" ]; then
      # The two-step conversion is paranoia against the filenames being encoded
      # inside the database or logfiles (db3 does this, for example).
      rm -f "${target}.flat"
      if [ "$old_db" = "flat" ]; then
        cp -a "$target" "${target}.flat"
      else
        $cvt_cyrusdb "$target" "$old_db" "${target}.flat" flat
      fi
      RETVAL=$?
      ERRVAL=$[ $ERRVAL + $RETVAL ]
      if [ $RETVAL -eq 0 ]; then
        rm -f "$target"
        if [ -s "${target}.flat" ]; then
          if [ "$new_db" = "flat" ]; then
            cp -a "${target}.flat" "$target"
          else
            $cvt_cyrusdb "${target}.flat" flat "$target" "$new_db"
          fi
        fi
        RETVAL=$?
        ERRVAL=$[ $ERRVAL + $RETVAL ]
        if [ $RETVAL -eq 0 ]; then
          rm -f "${target}.flat"
        else
          echo "ERROR: unable to convert ${target}.flat from flat to $new_db"
        fi
      else
        echo "ERROR: unable to convert $target from $old_db to flat"
      fi
    fi
  fi
}

ERRVAL=0

# convert all db files
cvt_file $imap_prefix/mailboxes.db    "$CONFIG_DB_MBOX"
cvt_file $imap_prefix/deliver.db      "$CONFIG_DB_DUPLICATE"
cvt_file $imap_prefix/tls_sessions.db "$CONFIG_DB_TLS"

find $imap_prefix/user/ -name "*.seen" -type f | while read db_file trash; do
  cvt_file "$db_file" "$CONFIG_DB_SEEN"
done

find $imap_prefix/user/ -name "*.sub" -type f | while read db_file trash; do
  cvt_file "$db_file" "$CONFIG_DB_SUBS"
done

# copy the current config file so we can check whether something has changed
if [ $ERRVAL -eq 0 ]; then
  cp $db_cfg $imap_prefix/rpm/db.cfg.cache
else
  rm -f $imap_prefix/rpm/db.cfg.cache
fi

exit $ERRVAL
