#!/bin/bash

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# This script migrates all db file from cyrus v2.x to cyrus v3.x.
# Since BDB support was dropped in v3.x, it is run only once in post install.
# Ths script is based on the old cvt_cyrusdb_all script by:
# Simon Matter, Invoca Systems <simon.matter@invoca.ch>

VERSION=3.0.1

PIDFILE=/var/run/cyrus-master${INSTANCE}.pid

# instance config
CYRUSCONF=/etc/cyrus${INSTANCE}.conf
IMAPDCONF=/etc/imapd${INSTANCE}.conf

# make sure what we have is a valid instance
# and that config files are present
[ -f $CYRUSCONF ] || exit 0
[ -f $IMAPDCONF ] || exit 0

if [ -f $PIDFILE ]; then
  read CYRUS_PID < $PIDFILE
  if [ -n "$CYRUS_PID" ]; then
    if ps -p $CYRUS_PID > /dev/null 2>&1; then
      echo "ERROR: cyrus-master is running, unable to convert mailboxes!"
      exit 1
    fi
  fi
fi

if [ ! -f $IMAPDCONF ]; then
  echo "ERROR: configuration file '${IMAPDCONF}' not found, exiting!"
  exit 1
fi

# fallback to su if runuser not available
if [ -x /sbin/runuser ]; then
  RUNUSER=runuser
else
  RUNUSER=su
fi

# force cyrus user for security reasons
if [ ! $(whoami) = "cyrus" ]; then
  exec $RUNUSER - cyrus -c "cd $PWD < /dev/null ; INSTANCE=$INSTANCE $0 $*"
fi

# special function for migration
EXPORT=$1

# files get mode 0600
umask 166

# show version info in log files
echo "migrate_cyrusdb version: $VERSION"

# expand_config <path>
# handle "@include" sections from imapd style config file
expand_config() {
  while read line; do
    if printf "%s\n" "${line}" | grep -q '^@include:'; then
      expand_config "$( printf "%s\n" "${line}" | cut -d : -f 2- | sed -e 's/^[\t ]*//' )"
    else
      printf "%s\n" "${line}"
    fi
  done < $1
}

# get_config <config> [<default>]
# extracts config option from config file
get_config() {
  searchstr=$1
  if config="$(expand_config $IMAPDCONF | egrep "^${searchstr}:")"; then
    CFGVAL="$(printf "%s\n" "$config" | cut -d : -f 2- | sed -e 's/^[\t ]*//')"
  else
    if [ -z "$2" ]; then
      echo "ERROR: config option '$1' not found in ${IMAPDCONF}, exiting!" 1>&2
      return 1
    fi
    CFGVAL="$2"
  fi
  echo "get_config ${1}: $CFGVAL" 1>&2
  echo "$CFGVAL"
}

# where to find files and directories
data_dir=/usr/share/cyrus-imapd/rpm
lib_dir=/usr/lib/cyrus-imapd
system_magic=$(file --version | awk '/magic file/ {print $4}')
cyrus_magic=${data_dir}/magic
cvt_cyrusdb=${lib_dir}/cvt_cyrusdb
sievec=${lib_dir}/sievec
masssievec=${lib_dir}/masssievec
imap_prefix=$(get_config configdirectory) || exit 1
sieve_dir=$(get_config sievedir) || exit 1
db_cfg=${data_dir}/db.cfg

# source default db backend config
. $db_cfg

# get configured db backend config
duplicate_db=$(get_config duplicate_db $duplicate_db) || exit 1
mboxlist_db=$(get_config mboxlist_db $mboxlist_db) || exit 1
seenstate_db=$(get_config seenstate_db $seenstate_db) || exit 1
subscription_db=$(get_config subscription_db $subscription_db) || exit 1
tlscache_db=$(get_config tlscache_db $tlscache_db) || exit 1
annotation_db=$(get_config annotation_db $annotation_db) || exit 1
mboxkey_db=$(get_config mboxkey_db $mboxkey_db) || exit 1
ptscache_db=$(get_config ptscache_db $ptscache_db) || exit 1
quota_db=$(get_config quota_db $quota_db) || exit 1
statuscache_db=$(get_config statuscache_db $statuscache_db) || exit 1
userdeny_db=$(get_config userdeny_db $userdeny_db) || exit 1

# file_type <file>
file_type() {
  this_type=$(file -b -m "${cyrus_magic}:${system_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 twoskip > /dev/null 2>&1; then
    echo twoskip
  elif echo "$this_type" | grep -qi text > /dev/null 2>&1; then
    echo flat
  else
    echo berkeley
  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 (berkeley does this, for example).
      rm -f "${target}.skiplist"
      if [ "$old_db" = "skiplist" ]; then
        cp -a "$target" "${target}.skiplist"
      else
        $cvt_cyrusdb -C $IMAPDCONF "$target" "$old_db" "${target}.skiplist" skiplist
      fi
      RETVAL=$?
      ERRVAL=$(( $ERRVAL + $RETVAL ))
      if [ $RETVAL -eq 0 ]; then
        rm -f "$target"
        if [ -s "${target}.skiplist" ]; then
          if [ "$new_db" = "skiplist" ]; then
            cp -a "${target}.skiplist" "$target"
          else
            $cvt_cyrusdb -C $IMAPDCONF "${target}.skiplist" skiplist "$target" "$new_db"
          fi
        fi
        RETVAL=$?
        ERRVAL=$(( $ERRVAL + $RETVAL ))
        if [ $RETVAL -eq 0 ]; then
          rm -f "${target}.skiplist"
        else
          echo "ERROR: unable to convert ${target}.skiplist from skiplist to $new_db"
        fi
      else
        echo "ERROR: unable to convert $target from $old_db to skiplist"
      fi
    fi
  fi
}

# cvt_to_utf8 <file>
cvt_to_utf8() {
  target="$1"
  if [ -s "$target" ]; then
    if ! $sievec -C $IMAPDCONF "$target" "${target}.sievec"; then
      iconv --from-code=ISO-8859-1 --to-code=UTF-8 --output="${target}.UTF-8" "$target"
      if [ -s "${target}.UTF-8" ]; then
        # preserve timestamp
        touch --reference="${target}" "${target}.UTF-8"
        mv -f "${target}.UTF-8" "$target"
      else
        ERRVAL=$(( $ERRVAL + 1 ))
      fi
    fi
    rm -f "${target}.sievec"
  fi
}

ERRVAL=0

# make sure our Berkeley databases are in a sane state
# wait for db_checkpoint to end successfully or kill it after a timeout
db_checkpoint -v -1 -h ${imap_prefix}/db &
DB_CHECK_PID=$!
CNT=0
while [ $CNT -lt 60 ]; do
  if ! kill -0 $DB_CHECK_PID > /dev/null 2>&1; then
    break
  fi
  sleep 1
  let CNT+=1
done
if kill -0 $DB_CHECK_PID > /dev/null 2>&1; then
  kill -USR1 $DB_CHECK_PID > /dev/null 2>&1
  sleep 1
  kill -KILL $DB_CHECK_PID > /dev/null 2>&1
  wait $DB_CHECK_PID > /dev/null 2>&1
fi

# do a normal recovery
db_recover -v -h ${imap_prefix}/db
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
  # try a catastrophic recovery instead of normal recovery
  db_recover -v -c -h ${imap_prefix}/db
  RETVAL=$?
  ERRVAL=$(( $ERRVAL + $RETVAL ))
  if [ $RETVAL -ne 0 ]; then
    echo "ERROR: catastrophic recovery of Cyrus databases failed"
  fi
fi

# always convert db files which have been converted to skiplist
# TODO: quota_db, we don't touch it for now
cvt_file ${imap_prefix}/mailboxes.db         "$mboxlist_db"
cvt_file ${imap_prefix}/annotations.db       "$annotation_db"
cvt_file ${imap_prefix}/user_deny.db         "$userdeny_db"
# remove obsolete DBs (now in /var/run/cyrus/ and will always be recreated)
rm -rf ${imap_prefix}/{tls_sessions,statuscach,deliver}.db ${imap_prefix}/ptclient
# do we have to convert all databases?
# we treat sieve scripts the same way like db files
find ${sieve_dir}/ -name "*.script" -type f | while read db_file trash; do
  cvt_to_utf8 "$db_file"
done
$masssievec $sievec $IMAPDCONF
# convert all db files left
find ${imap_prefix}/user/ -name "*.seen" -type f | while read db_file trash; do
  cvt_file "$db_file" "$seenstate_db"
done
find ${imap_prefix}/user/ -name "*.sub" -type f | while read db_file trash; do
  cvt_file "$db_file" "$subscription_db"
done
find ${imap_prefix}/user/ -name "*.mboxkey" -type f | while read db_file trash; do
  cvt_file "$db_file" "$mboxkey_db"
done

exit $ERRVAL
