# This file will be sourced by the shell bash.
#
# Filename: 20-get-sysinternals-helpers.bash
# Version: 1.0-beta-5
# Release date: 2017-08-25
# Intended compatibility: WSUS Offline Update Version 11.0.1 and newer
#
# Copyright (C) 2016-2017 Hartmut Buhrmester
#                         <zo3xaiD8-eiK1iawa@t-online.de>
#
# License
#
#     This file 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 3 of the License,
#     or (at your option) any later version.
#
#     This file is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     General Public License for more details.
#
#     You should have received a copy of the GNU General
#     Public License along with this program.  If not, see
#     <http://www.gnu.org/licenses/>.
#
# Description
#
#     This task downloads and installs the Sysinternals utilities Sigcheck
#     and Autologon.
#
#     Sigcheck may be used under wine to check the digital file
#     signatures, but this requires the necessary root certificates and
#     all certificate chains to be installed. This doesn't work so far.
#
#     Autologon is used during the installation of the updates.
#
#     The Windows script DownloadUpdates.cmd uses a third utility Streams,
#     which removes alternate data streams on NTFS volumes, but this is
#     not needed for Linux.


# ========== Global variables =============================================

# The variable sigcheck_bin must be global, because it is used in the
# file digital-file-signatures.bash
sigcheck_bin="sigcheck.exe"

# ========== Functions ====================================================

function get_sysinternals_helpers ()
{
    # It is recommended to use two steps to define a local variable and
    # to assign a value with a command substitution, because otherwise,
    # the built-in command "local" would mask the return code of the
    # command substitution.
    local autologon_link=""
    autologon_link="$(grep_dos -F -i autologon.zip ../static/StaticDownloadLinks-sysinternals.txt)" || true
    # Use a secure connection, because we can.
    local autologon_link="${autologon_link/http:/https:}"
    local autologon_archive="${autologon_link##*/}"
    local autologon_bin="Autologon.exe"

    local sigcheck_link=""
    sigcheck_link="$(grep_dos -F -i sigcheck.zip ../static/StaticDownloadLinks-sysinternals.txt)" || true
    local sigcheck_link="${sigcheck_link/http:/https:}"
    local sigcheck_archive="${sigcheck_link##*/}"

    local initial_errors="${runtime_errors}"

    # Get Sysinternals Autologon
    if [[ ! -f ../client/bin/"${autologon_bin}" ]]; then
        log_info_message "Downloading Sysinternals Autologon.exe ..."
        download_single_file ../client/bin "${autologon_link}"
        if (( runtime_errors == initial_errors )); then
            log_info_message "Unpacking Sysinternals Autologon.exe ..."
            if unzip ../client/bin/"${autologon_archive}" "${autologon_bin}" -d ../client/bin; then
                trash_file ../client/bin/"${autologon_archive}"
                log_info_message "Done"
            else
                log_error_message "Unpacking of Autologon.exe failed"
            fi
        else
            log_error_message "Download of Autologon.exe failed"
        fi
        echo ""
    fi

    # Get Sysinternals Sigcheck
    initial_errors="${runtime_errors}"
    if [[ ! -f ../bin/"${sigcheck_bin}" ]]; then
        log_info_message "Downloading Sysinternals sigcheck.exe ..."
        download_single_file ../bin "${sigcheck_link}"
        if (( runtime_errors == initial_errors )); then
            log_info_message "Unpacking Sysinternals sigcheck.exe ..."
            if unzip ../bin/"${sigcheck_archive}" "${sigcheck_bin}" -d ../bin; then
                trash_file ../bin/"${sigcheck_archive}"
                log_info_message "Done"
            else
                log_error_message "Unpacking of sigcheck.exe failed"
            fi
        else
            log_error_message "Download of sigcheck.exe failed"
        fi
        echo ""
    fi
    return 0
}

# ========== Commands =====================================================

get_sysinternals_helpers
return 0
