#!/bin/bash
#
#Server-Manager: o_encrypt (encrypted files) [Power: 700]
#
#Version: 1.3.5
#Date:    2026.10.03
#Unix:    Debian 11
#Author:  P.Klapp
#Company: Omoti UG (haftungsbeschränkt)
#Website: https://omoti.de
#
#Path:    /Commands/


#Version
    version=$(awk '$1 == "#Version:" { print $2; exit }' "$0")

#Parameters
    para_1=${1}

#Functions
#Dependencies
    #parameters
        if [[ -z ${para_1} ]]; then
            echo -e "\033[33mo_encrypt: missing source\033[0m"
            echo -e "Try '\033[36mo_encrypt --help\033[0m' or '\033[36mo_encrypt -?\033[0m' for more information"
            exit 1

        elif [[ ${para_1} == '-k' ]]; then
            if [[ -f /srv/omoti/keys/gpg/Omoti-Server_public.asc ]]; then
                echo 'Key is already present'
                exit 0

            else
                if ! [[ -d /srv/omoti/keys/gpg/ ]]; then
                    mkdir -p /srv/omoti/keys/gpg/
                fi

                cd /srv/omoti/keys/gpg/ || exit 1
                if ! o_download -a https://mgmt.omoti.de/Keys/GPG/Omoti-Server_public.asc
                then
                    echo -e "\033[31mFailed to download the Omoti encryption key\033[0m"
                    exit 1
                fi
                if ! gpg --import Omoti-Server_public.asc
                then
                    echo -e "\033[31mFailed to import the Omoti encryption key\033[0m"
                    exit 1
                fi
                #rm Omoti-Server_public.asc
                echo -e "5\ny\n" | gpg --command-fd 0 --expert --edit-key Omoti-Server trust 2>&1
                echo 'Omoti-Server_public.asc was imported'
                o_log -i 'Omoti-Server_public.asc was imported'
                exit 0
            fi

        elif [[ ${para_1} == '-?' ]] || [[ ${para_1} == '--help' ]]; then
            echo -e 'Usage:\033[36m o_encrypt SOURCE...\033[0m'
            echo "o_encrypt (v${version}) encrypted files"
            echo 'The source must be in one of the following formats: bz2, gz, zip, zst'
            echo ''
            echo 'Options:'
            echo '-k                        imports the Omoti-Server key'
            echo '-? / --help               display this help and exit'
            echo ''
            echo 'Full documentation can be found in the Omoti Wiki'
            exit 0

        else
            source="${para_1}"
        fi

    #source type (bz2-file / zip-file)

        if [[ -f ${source} ]]; then
            sourceType=${source##*.}

            if [[ ${sourceType} != 'bz2' ]] && [[ ${sourceType} != 'gz' ]] && [[ ${sourceType} != 'zip' ]] && [[ ${sourceType} != 'zst' ]]; then
                echo -e "\033[33mo_encrypt: source in the wrong format\033[0m"
                echo -e "Try '\033[36mo_encrypt --help\033[0m' or '\033[36mo_encrypt -?\033[0m' for more information"
                exit 1
            fi

        else
            echo -e "\033[33mo_encrypt: unrecognized source '${source}'\033[0m"
            echo -e "Try '\033[36mo_encrypt --help\033[0m' or '\033[36mo_encrypt -?\033[0m' for more information"
            exit 1
        fi

#Variables
    directory=$(dirname -- "${source}")
    file=$(basename -- "${source}")
    #para_1         -> Parameters
    #source         -> Dependencies/parameters
    #sourceType     -> Dependencies/source type
    #version        -> Version

#Main
    if ! cd "${directory}"
    then
        o_log -e "Encryption failed because the source directory is unavailable: ${source}"
        exit 1
    fi

    if ! gpg --batch --yes --output "${file}.gpg" --encrypt --recipient Omoti-Server -- "${file}"
    then
        rm -f -- "${file}.gpg"
        o_log -w "Encryption failed: ${source}"
        exit 1
    fi

    if [[ ! -s ${file}.gpg ]]
    then
        o_log -w "Encryption failed because the encrypted file is missing or empty: ${source}"
        exit 1
    fi

    if ! rm -- "${file}"
    then
        o_log -e "Encryption succeeded but the source file could not be removed: ${source}"
        exit 1
    fi

    o_log -i "File encrypted ${file}.gpg"
