#!/bin/sh
#------------------------------------------------------------------------------
# /var/install/bin/subversion-tools-delrepo - delete repository
#
# 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
. /var/install/include/configlib
. /etc/config.d/subversion

modified="0"

#-----------------------------------------------------------------------------
# Update subversion configuration if necessary
#-----------------------------------------------------------------------------

update_config()
{
    if [ "${modified}" = "1" ]
    then
        /var/install/config.d/subversion.sh
        anykey
    fi
}

#-----------------------------------------------------------------------------
# Remove config entry $1 from disk and config
#-----------------------------------------------------------------------------

remove_repo()
{
    local idx1="$1"

    # remove from disk
    eval repo_dir='${SVN_REPOS_'${idx1}'_DIR}'
    rm -rf "${repo_dir}"
    rm -f  /var/lib/subversion/userdb${SVN_REPOS_N}
    rm -f  /var/lib/subversion/htpasswd${SVN_REPOS_N}

    # remove from config
    while [ "${idx1}" -lt "${SVN_REPOS_N}" ]
    do
        local idx2=$((${idx1} + 1))

        eval SVN_REPOS_${idx1}_ACTIVE='${SVN_REPOS_'${idx2}'_ACTIVE}'
        eval SVN_REPOS_${idx1}_NAME='${SVN_REPOS_'${idx2}'_NAME}'
        eval SVN_REPOS_${idx1}_DIR='${SVN_REPOS_'${idx2}'_DIR}'
        eval SVN_REPOS_${idx1}_WEBDAV='${SVN_REPOS_'${idx2}'_WEBDAV}'
        eval SVN_REPOS_${idx1}_ANON_ACCESS='${SVN_REPOS_'${idx2}'_ANON_ACCESS}'
        eval SVN_REPOS_${idx1}_AUTH_ACCESS='${SVN_REPOS_'${idx2}'_AUTH_ACCESS}'
        eval SVN_REPOS_${idx1}_ACL='${SVN_REPOS_'${idx2}'_ACL}'
        eval SVN_REPOS_${idx1}_BACKUP='${SVN_REPOS_'${idx2}'_BACKUP}'
        eval SVN_REPOS_${idx1}_MAIL_COMMIT='${SVN_REPOS_'${idx2}'_MAIL_COMMIT}'
        eval SVN_REPOS_${idx1}_MAIL_ADDR='${SVN_REPOS_'${idx2}'_MAIL_ADDR}'
        eval SVN_REPOS_${idx1}_DIR_PERMISSIONS='${SVN_REPOS_'${idx2}'_DIR_PERMISSIONS}'
        eval SVN_REPOS_${idx1}_DIR_PERM_N='${SVN_REPOS_'${idx2}'_DIR_PERM_N}'

        eval num_perm='${SVN_REPOS_'${idx1}'_DIR_PERM_N}'
        local idx3=1

        while [ "${idx3}" -le "${num_perm}" ]
        do
            eval SVN_REPOS_${idx1}_DIR_${idx3}_PATH='${SVN_REPOS_'${idx2}'_DIR_'${idx3}'_PATH}'
            eval SVN_REPOS_${idx1}_DIR_${idx3}_PERM='${SVN_REPOS_'${idx2}'_DIR_'${idx3}'_PERM}'

            idx3=$((${idx3} + 1))
        done

        idx1="${idx2}"
    done

    SVN_REPOS_N=$((${SVN_REPOS_N} - 1))
}


#-----------------------------------------------------------------------------
# Rewrite config file
#-----------------------------------------------------------------------------

rewrite_config_file()
{
    # set default values
    if [ "${SVN_ACL_N}" = "0" ]
    then
        SVN_ACL_1_NAME='GLOBAL'                   # Name of access list
        SVN_ACL_1_USER_N='1'                      # Number of users in this list
        SVN_ACL_1_USER_1_ACTIVE='yes'             # Is user active?
        SVN_ACL_1_USER_1_NAME='svnuser'           # Name of user
        SVN_ACL_1_USER_1_PASSWD='geheim'          # User's password
    fi

    if [ "${SVN_REPOS_N}" = "0" ]
    then
        SVN_REPOS_1_ACTIVE='yes'                  # Is this repository active
        SVN_REPOS_1_DIR='/var/lib/svn'            # Root path of n'th repo
        SVN_REPOS_1_WEBDAV='no'                   # Enable HTTP WebDAV access for repo
        SVN_REPOS_1_ANON_ACCESS='none'            # Access rights for anonymous users
        SVN_REPOS_1_AUTH_ACCESS='write'           # Access rights for auth. users
        SVN_REPOS_1_ACL='GLOBAL'                  # ACLs separated by by spaces
        SVN_REPOS_1_BACKUP='no'                   # Automatic backup yes or no
        SVN_REPOS_1_MAIL_COMMIT='no'              # Send notifications upon commit
        SVN_REPOS_1_MAIL_ADDR='commit@somewhere'  # Mail address to send email to
        SVN_REPOS_1_DIR_PERMISSIONS='no'          # Use directory permissions
        SVN_REPOS_1_DIR_PERM_N='0'                # Number of directory permissions
        SVN_REPOS_1_DIR_1_PATH='/'                # Repository path
        SVN_REPOS_1_DIR_1_PERM='*=r'              # Access Permissions
    fi

    # write config file
    (
    printgpl -conf "${packages_name}" "2019-01-13" "dv" \
         "2025 the eisfair team, team(at)eisfair(dot)org"

    printgroup "Global enable"
    printvar   "START_SUBVERSION" "Shall subversion be enabled?"

    printgroup "Subversion server settings"
    printvar   "SVN_ENABLE_SVNSERVE" "Shall subversion be accessible through svnserve?"
    printvar   "SVN_SVNSERVE_ROOT_DIR" "common root directory for svnserve"
    printvar   "SVN_ENABLE_WEBDAV" "Shall subversion be accessible over HTTP?"
    echo ""

    printgroup "Subversion user ACLs"
    printvar   "SVN_ACL_N" "Number of defined access lists"
    printvar   "SVN_ACL_1_NAME" "Name of access list"
    printvar   "SVN_ACL_1_USER_N" "Number of users in this list"
    printvar   "SVN_ACL_1_USER_1_ACTIVE" "Is user active"
    printvar   "SVN_ACL_1_USER_1_NAME" "Name of user"
    printvar   "SVN_ACL_1_USER_1_PASSWD" "User's password"
    iidx=2
    while [ "${iidx}" -le "${SVN_ACL_1_USER_N}" ]
    do
        echo ""
        printvar "SVN_ACL_1_USER_${iidx}_ACTIVE" "Is user active"
        printvar "SVN_ACL_1_USER_${iidx}_NAME" "Name of user"
        printvar "SVN_ACL_1_USER_${iidx}_PASSWD" "User's password"
        iidx=$((${iidx} + 1))
    done

    idx=2
    while [ "${idx}" -le "${SVN_ACL_N}" ]
    do
        echo ""
        printvar "SVN_ACL_${idx}_NAME" "Name of access list"
        printvar "SVN_ACL_${idx}_USER_N" "Number of users in this list"

        eval num_users='${SVN_ACL_'${idx}'_USER_N}'
        iidx=1
        while [ "${iidx}" -le "${num_users}" ]
        do
            echo ""
            printvar "SVN_ACL_${idx}_USER_${iidx}_ACTIVE" "Is user active"
            printvar "SVN_ACL_${idx}_USER_${iidx}_NAME" "Name of user"
            printvar "SVN_ACL_${idx}_USER_${iidx}_PASSWD" "User's password"
            iidx=$((${iidx} + 1))
        done
        idx=$((${idx} + 1))
    done

    printgroup "Subversion repository settings"
    printvar   "SVN_REPOS_N" "Number of repositories"
    echo       ""
    printvar   "SVN_REPOS_1_ACTIVE"       "Is the repository active?"
    printvar   "SVN_REPOS_1_NAME"         "Friendly name for WebDAV URL: /svn/\$NAME and e-mail notify"
    printvar   "SVN_REPOS_1_DIR"          "Root path of n'th repo"
    printvar   "SVN_REPOS_1_WEBDAV"       "Enable HTTP WebDAV access for repo"
    printvar   "SVN_REPOS_1_ANON_ACCESS"  "Access rights for anonymous users"
    printvar   "SVN_REPOS_1_AUTH_ACCESS"  "Access rights for auth. users"
    printvar   "SVN_REPOS_1_ACL"          "ACLs separated by by spaces"
    printvar   "SVN_REPOS_1_BACKUP"       "Automatic backup yes or no"
    printvar   "SVN_REPOS_1_MAIL_COMMIT"  "Send notifications upon commit"
    printvar   "SVN_REPOS_1_MAIL_ADDR"    "Mail address to send email to"
    printvar   "SVN_REPOS_1_DIR_PERMISSIONS" "Use directory permissions"
    printvar   "SVN_REPOS_1_DIR_PERM_N"      "Number of directory permisions"
    echo       ""
    printvar   "SVN_REPOS_1_DIR_1_PATH"   "Repository path"
    printvar   "SVN_REPOS_1_DIR_1_PERM"   "Permissions for this path"

    idx2=2
    while [ "${idx2}" -le "${SVN_REPOS_1_DIR_PERM_N}" ]
    do
        echo    ""
        printvar "SVN_REPOS_1_DIR_${idx2}_PATH" "Repository path"
        printvar "SVN_REPOS_1_DIR_${idx2}_PERM" "Permissions for this path"

        idx2=$((${idx2} + 1))
    done

    idx=2
    while [ "${idx}" -le "${SVN_REPOS_N}" ]
    do
        echo     ""
        printvar "SVN_REPOS_${idx}_ACTIVE"          "Is the repository active?"
        printvar "SVN_REPOS_${idx}_NAME"            "Friendly name for WebDAV URL: /svn/\$NAME and e-mail notify"
        printvar "SVN_REPOS_${idx}_DIR"             "Root path of n'th repo"
        printvar "SVN_REPOS_${idx}_WEBDAV"          "Enable HTTP WebDAV access for repo"
        printvar "SVN_REPOS_${idx}_ANON_ACCESS"     "Access rights for anonymous users"
        printvar "SVN_REPOS_${idx}_AUTH_ACCESS"     "Access rights for auth. users"
        printvar "SVN_REPOS_${idx}_ACL"             "ACLs separated by by spaces"
        printvar "SVN_REPOS_${idx}_BACKUP"          "Automatic backup yes or no"
        printvar "SVN_REPOS_${idx}_MAIL_COMMIT"     "Send notifications upon commit"
        printvar "SVN_REPOS_${idx}_MAIL_ADDR"       "Mail address to send email to"
        printvar "SVN_REPOS_${idx}_DIR_PERMISSIONS" "Use directory permissions"
        printvar "SVN_REPOS_${idx}_DIR_PERM_N"      "Number of directory permisions"

        eval numperm='${SVN_REPOS_'${idx}'_DIR_PERM_N}'
        idx2=1
        while [ "${idx2}" -le "${numperm}" ]
        do
            echo    ""
            printvar "SVN_REPOS_${idx}_DIR_${idx2}_PATH" "Repository path"
            printvar "SVN_REPOS_${idx}_DIR_${idx2}_PERM" "Permissions for this path"

            idx2=$((${idx2} + 1))
        done

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

    printgroup "Subversion backup settings"
    printvar   "SVN_BACKUP_SCHEDULE" "Cron schedule for autom. backup"
    printvar   "SVN_BACKUP_TARGET" "Target directory for backups"
    printvar   "SVN_BACKUP_MAXFILES" "Max. number of backups to keep"
    printvar   "SVN_BACKUP_MOUNT" "Command executed before backup"
    printvar   "SVN_BACKUP_UMOUNT" "Command executed after backup"
    printvar   "SVN_BACKUP_NOTIFY" "E-mail address for error notification"

    printend
    ) > /etc/config.d/subversion
    # Set rights
    chmod 0644 /etc/config.d/subversion
    chown root /etc/config.d/subversion
}


#-----------------------------------------------------------------------------
# Perform deletion
#-----------------------------------------------------------------------------

while true
do
    clrhome

    if [ "${SVN_REPOS_N}" = '0' ]
    then
        mecho ""
        mecho -error "Error: There are no repositories defined on this server!"
        anykey
        update_config
        exit 1
    fi

    mecho ""
    mecho -info "Please select repository to remove from config and disk:"
    mecho ""

    techo begin 4 12 52
    techo row -info No -info Name -info Path
    idx="1"
    while [ "${idx}" -le "${SVN_REPOS_N}" ]
    do
        eval repodir='${SVN_REPOS_'${idx}'_DIR}'
        eval reponame='${SVN_REPOS_'${idx}'_NAME}'

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

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

    mecho ""
    del_repo=$(/var/install/bin/ask "Select (0 = Return)" "${del_repo}" "0-${SVN_REPOS_N}")
    if [ "${del_repo}" = "0" ]
    then
        update_config
        exit 0
    fi

    mecho
    mecho -warn "ALL DATA WILL BE LOST!"
    if /var/install/bin/ask "Do you really want to remove repository \"${del_repo}\"" "yes"
    then
        mecho -info -n "deleting... "
        remove_repo "${del_repo}"
        rewrite_config_file
        mecho "done"

        modified="1"
    fi

    mecho ""

    if [ "${SVN_REPOS_N}" = '0' ]
    then
        mecho "No more repositories left!"
        anykey
        update_config
        exit 0
    fi

    if ! /var/install/bin/ask "Delete one more repository?" "no"
    then
        update_config
        exit 0
    fi
done


#-----------------------------------------------------------------------------
# End
#-----------------------------------------------------------------------------

exit 0