#!/bin/sh
#------------------------------------------------------------------------------
# /var/install/bin/subversion-tools-upgrade - upgrade repository to current version
#
# Creation   :  2018-07-11 daniel
# Last Update:  2023-07-23 09:11:10
#
# Copyright (c) 2025 the eisfair team, team(at)eisfair(dot)org
#
# 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.
#------------------------------------------------------------------------------

#include eislib
. /var/install/include/eislib
. /etc/config.d/subversion

PATH='/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin'
PROG="svnadmin"
CREATEIDX="svn-populate-node-origins-index"
FS_FORMAT="8"

clrhome

# select repository
mecho "Here you can upgrade the file format of the repository to the"
mecho "current version."
mecho
mecho -warn "Please note: When you have upgraded from an older version, you"
mecho -warn "still can't use all features introduced with subversion 1.10 (reduced)"
mecho -warn "To get full functionality and improved performace, you need to backup"
mecho -warn "and restore the data of your repositories instead."
mecho
mecho -info "Select repository to upgrade:"

techo begin 4 12 40 24
techo row -info No -info Name -info Path -info Status

# list all configured repositories and show update status
idx="1"
while [ "${idx}" -le "${SVN_REPOS_N}" ]
do
    eval repodir='${SVN_REPOS_'${idx}'_DIR}'
    eval reponame='${SVN_REPOS_'${idx}'_NAME}'

    if [ -f "${repodir}/db/format" ]
    then
        format=$(cat "${repodir}/db/format" | head -n 1)

        if [ "${format}" -lt "${FS_FORMAT}" ]
        then
            repostate="old (pre 1.10)"
        elif [ "${format}" -gt "${FS_FORMAT}" ]
        then
            repostate="more recent!"
        else
            repostate="up to date"
        fi

        if [ "${repostate}" = "up to date" ]
        then
            if svnadmin info "${repodir}" | grep -q "FSFS Logical Addressing: yes"
            then
                repostate="up to date (full)"
            else
                repostate="up to date (reduced)"
            fi
        fi
    else
        repostate="missing"
    fi

    techo row "${idx}" "${reponame}" "${repodir}" "${repostate}"

    idx=$((${idx} + 1))
done
techo end

# query selection from user
mecho ""
sel_repo=$(/var/install/bin/ask "Select (0 = cancel)" "$sel_repo" "0-${SVN_REPOS_N}")

if [ "${sel_repo}" = "0" ]
then
    mecho -info "command canceled"
    anykey
    exit 0
else
    mecho ""
    eval repodir='${SVN_REPOS_'${sel_repo}'_DIR}'
    eval reponame='${SVN_REPOS_'${sel_repo}'_NAME}'
    mecho -n -info "Upgrade of repository: "
    mecho "${reponame}"

    {
        read repover
    } < "${repodir}/db/format"

    if [ "${repover}" -ge "${FS_FORMAT}" ]
    then
        mecho
        mecho -info "This repository already is up to date! No upgrade required!"
        mecho
        anykey
        exit 0
    fi

    ${PROG} upgrade ${repodir} > /dev/null
    if [ "$?" = "0" ]
    then
        /var/install/bin/subversion-tools-permissions ${repodir}

        if [ "${repover}" -lt "3" ]
        then
            ${CREATEIDX} "${repodir}"
            if [ "$?" != "0" ]
            then
                mecho ""
                mecho -warn "populate-node-origins-index failed!"
                mecho ""
                anykey
                exit 1
            fi
        fi
        mecho ""
        mecho "Upgrade completed successfully!"
        mecho ""
    else
        mecho ""
        mecho -warn "Upgrade failed!"
        mecho ""
        anykey
        exit 1
    fi

    anykey
    exit 0
fi