#!/bin/bash

ver=$(dpkg -l|grep scylla-server|awk '{print $3}'|sed -e "s/-.*$//")
if [ -n "$ver" ]; then
    ver_fmt=$(echo $ver | awk -F. '{printf "%d%02d%02d", $1,$2,$3}')
    if [ $ver_fmt -lt 10703 ]; then
        # for <scylla-1.2
        if [ ! -f /opt/scylladb/scripts/scylla_config_get.py ]; then
            echo
            echo "Error: Upgrading from scylla-$ver to this version of scylla is not supported."
            echo "Please upgrade to scylla-1.7.3 or later, before upgrade to this version."
            echo
            exit 1
        fi
        commitlog_directory=$(/opt/scylladb/scripts/scylla_config_get.py -g commitlog_directory)
        commitlog_files=$(ls $commitlog_directory | wc -l)
        if [ $commitlog_files -ne 0 ]; then
            echo
            echo "Error: Upgrading from scylla-$ver to this version of scylla is not supported when commitlog is not clean."
            echo "Please upgrade to scylla-1.7.3 or later, before upgrade to this version."
            echo "Also make sure $commitlog_directory is empty."
            echo
            exit 1
        fi
    fi
fi

#DEBHELPER#
