#!/bin/bash
# Copyright 2020 Sebastian Wiesnser <sebastian@swsnr.de>

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

set -exuo pipefail

VERSION="$1"
TARGET="$2"

ARCHIVE_NAME="mdcat-${VERSION}-${TARGET}"

package_windows() {
    local archive_file
    archive_file="${ARCHIVE_NAME}.zip"
    mv LICENSE LICENSE.txt
    7z a "${archive_file}" \
        "./target/${TARGET}/release/mdcat.exe" \
        ./README.md ./CHANGELOG.md ./LICENSE.txt ./mdcat.1.adoc
    if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
        echo "file=${archive_file}" >>"${GITHUB_OUTPUT}"
        echo "name=${ARCHIVE_NAME}.zip" >>"${GITHUB_OUTPUT}"
    fi
}

package_unix() {
    local archive_file
    local pkg_dir
    archive_file="${ARCHIVE_NAME}.tar.gz"

    pkg_dir=$(mktemp -d)

    mkdir "${pkg_dir}/${ARCHIVE_NAME}"
    cp -r \
        README.md CHANGELOG.md LICENSE \
        "target/${TARGET}/release/mdcat" \
        "mdcat.1" \
        "${pkg_dir}/${ARCHIVE_NAME}"
    tar -czf "${PWD}/${archive_file}" -C "${pkg_dir}" "${ARCHIVE_NAME}"
    echo "file=${archive_file}" >>"${GITHUB_OUTPUT}"
    echo "name=${ARCHIVE_NAME}.tar.gz" >>"${GITHUB_OUTPUT}"
}

if [[ "${TARGET}" == *windows* ]]; then
    package_windows
else
    package_unix
fi
