#!/bin/bash
#
#Server-Manager: o_download (System Command: Downloads a file) [Power: 755]
#
#Version: 1.2.0
#Date:    2022.06.10
#Unix:    Debian 11
#Author:  P.Klapp
#Company: Omoti UG (haftungsbeschränkt)
#Website: https://omoti.de
#
#Path:    /Commands/


#Version
    version=$(sed -n '5p' ${0})
    version=${version#*\ }

#Temporary file
    tmp="/srv/omoti/tmp/o_download.$(date +'%H%M%S')$(pwgen -s -0 -N 1 2).tmp"

#Parameters
    para_1=${1}
    para_2=${2}
    para_3=${3}
    para_4=${4}

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

        elif [[ ${para_1} == '-a' ]] || [[ ${para_1} == '-an' ]] || [[ ${para_1} == '-l' ]]; then
            option="${para_1}"

            #o_download -a $LINK
            if [[ ${option} == '-a' ]] && [[ ${para_2} != '-n' ]] && [[ ${para_2} ]]; then
                logEntry='true'
                source="${para_2}"

            #o_download -a -n $LINK
            elif [[ ${option} == '-a' ]] && [[ ${para_2} == '-n' ]] && [[ ${para_3} ]]; then
                logEntry='false'
                source="${para_3}"

            #o_download -an $LINK
            elif [[ ${option} == '-an' ]] && [[ ${para_2} ]]; then
                logEntry='false'
                source="${para_2}"

            #o_download -l $USER $PW $LINK
            elif [[ ${option} == '-l' ]] && [[ ${para_2} ]] && [[ ${para_3} ]] && [[ ${para_4} ]]; then
                logEntry='true'
                source="${para_4}"
                webUser="${para_2}"
                webPassword="${para_3}"

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

        elif [[ ${para_1} == '-?' ]] || [[ ${para_1} == '--help' ]]; then
            echo -e 'Usage:\033[36m o_download [OPTION]... SOURCE...\033[0m'
            echo "o_download (v${version}) Downloads a file"
            echo ''
            echo 'Options:'
            echo '-a                        Uses an Omoti access file for the login data'
            echo '-l <USER> <PASSWORD>      Manual entry of login data'
            echo '-n                        Log entry is suppressed (Can only be used in conjunction with -a)'
            echo '-? / --help               display this help and exit'
            echo ''
            echo 'Full documentation can be found in the Omoti Wiki'
            exit 0

        else
            logEntry='true'
            option='false'
            source="${para_1}"
        fi

    #access file
        if [[ ${option} == '-a' ]] || [[ ${option} == '-an' ]]; then
            if [[ -f /srv/omoti/keys/access/data.cfg ]]; then
                #user
                    dataUser=$(sed -n "$(sed -n '/-user/=' /srv/omoti/keys/access/data.cfg)p" /srv/omoti/keys/access/data.cfg)
                    dataUser=${dataUser#*=}

                #token
                    dataToken=$(sed -n "$(sed -n '/-token/=' /srv/omoti/keys/access/data.cfg)p" /srv/omoti/keys/access/data.cfg)
                    dataToken=${dataToken#*=}

                #controll
                    if [[ -z ${dataUser} ]] || [[ -z ${dataToken} ]]; then
                        echo -e "\033[31mError! No access data\033[0m"
                        exit 0
                    fi
            else
                echo -e "\033[31mError! No access config\033[0m"
                exit 0
            fi
        fi

#Variables
    #controll       -> Main/check download
    #dataToken      -> Dependencies/access file
    #dataUser       -> Dependencies/access file
    #logEntry       -> Dependencies/parameters
    #option         -> Dependencies/parameters
    #para_1         -> Parameters
    #para_2         -> Parameters
    #para_3         -> Parameters
    #para_4         -> Parameters
    #source         -> Dependencies/parameters
    targetFile=${source##*/}
    #tmp            -> Temporary file
    #version        -> Version
    #webPassword    -> Dependencies/parameters
    #webUser        -> Dependencies/parameters

#Main
    #download
        if [[ ${option} == '-a' ]]; then
            wget -q --user=${dataUser} --password=${dataToken} ${source}
        elif [[ ${option} == '-l' ]]; then
            wget -q --user=${webUser} --password=${webPassword} ${source}
        else
            wget -q ${source}
        fi

    #check download
        if [[ -f ${targetFile} ]]; then
            controll='true'
        else
            controll='false'
            echo -e "\033[33mWarning! Download failed\033[0m"
        fi

    #log entry
        if [[ ${controll} == 'true' ]]; then
            if [[ ${logEntry} != 'false' ]]; then
                :
                #o_log -d "${targetFile} was successfully downloaded"
            else
                o_log -w "Outdated syntax detected (-n): $(ps -o comm= $PPID)"
            fi
        else
            o_log -w "Download of file ${targetFile} failed"
        fi