#!/usr/bin/env bash

# build - build Jellyfin binaries or packages

set -o errexit
set -o pipefail

# The list of possible package actions (except 'clean')
declare -a actions=( 'build' 'package' 'sign' 'publish' )

# The list of possible platforms, based on directories under 'deployment/'
declare -a platforms=( $(
    find deployment/ -maxdepth 1 -mindepth 1 -type d -exec basename {} \; | sort
) )

# The list of standard dependencies required by all build scripts; individual
# action scripts may specify their own dependencies
declare -a dependencies=( 'tar' 'zip' )

usage() {
    echo -e "build - build Jellyfin binaries or packages"
    echo -e ""
    echo -e "Usage:"
    echo -e " $ build --list-platforms"
    echo -e " $ build --list-actions <platform>"
    echo -e " $ build [-k/--keep-artifacts] [-b/--web-branch <web_branch>] <platform> <action>"
    echo -e ""
    echo -e "The 'keep-artifacts' option preserves build artifacts, e.g. Docker images for system package builds."
    echo -e "The web_branch defaults to the same branch name as the current main branch or can be 'local' to not touch the submodule branching."
    echo -e "To build all platforms, use 'all'."
    echo -e "To perform all build actions, use 'all'."
    echo -e "Build output files are collected at '../bin/<platform>'."
}

# Show usage on stderr with exit 1 on argless
if [[ -z $1 ]]; then
    usage >&2
    exit 1
fi
# Show usage if -h or --help are specified in the args
if [[ $@ =~ '-h' || $@ =~ '--help' ]]; then
    usage
    exit 0
fi

# List all available platforms then exit
if [[ $1 == '--list-platforms' ]]; then
    echo -e "Available platforms:"
    for platform in ${platforms[@]}; do
        echo -e "  ${platform}"
    done
    exit 0
fi

# List all available actions for a given platform then exit
if [[ $1 == '--list-actions' ]]; then
    platform="$2"
    if [[ ! " ${platforms[@]} " =~ " ${platform} " ]]; then
        echo "ERROR: Platform ${platform} does not exist."
        exit 1
    fi
    echo -e "Available actions for platform ${platform}:"
    for action in ${actions[@]}; do
        if [[ -f deployment/${platform}/${action}.sh ]]; then
            echo -e "  ${action}"
        fi
    done
    exit 0
fi

# Parse keep-artifacts option
if [[ $1 == '-k' || $1 == '--keep-artifacts' ]]; then
    keep_artifacts="y"
    shift 1
else
    keep_artifacts="n"
fi

# Parse branch option
if [[ $1 == '-b' || $1 == '--web-branch' ]]; then
	web_branch="$2"
	shift 2
else
    web_branch="$( git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' )"
fi

# Parse platform option
if [[ -n $1 ]]; then
    cli_platform="$1"
    shift
else
    echo "ERROR: A platform must be specified. Use 'all' to specify all platforms."
    exit 1
fi
if [[ ${cli_platform} == 'all' ]]; then
    declare -a platform=( ${platforms[@]} )
else
    if [[ ! " ${platforms[@]} " =~ " ${cli_platform} " ]]; then
        echo "ERROR: Platform ${cli_platform} is invalid. Use the '--list-platforms' option to list available platforms."
        exit 1
    else
        declare -a platform=( "${cli_platform}" )
    fi
fi

# Parse action option
if [[ -n $1 ]]; then
    cli_action="$1"
    shift
else
    echo "ERROR: An action must be specified. Use 'all' to specify all actions."
    exit 1
fi
if [[ ${cli_action} == 'all' ]]; then
    declare -a action=( ${actions[@]} )
else
    if [[ ! " ${actions[@]} " =~ " ${cli_action} " ]]; then
        echo "ERROR: Action ${cli_action} is invalid. Use the '--list-actions <platform>' option to list available actions."
        exit 1
    else
        declare -a action=( "${cli_action}" )
    fi
fi

# Verify required utilities are installed
missing_deps=()
for utility in ${dependencies[@]}; do
    if ! which ${utility} &>/dev/null; then
        missing_deps+=( ${utility} )
    fi
done

# Error if we're missing anything
if [[ ${#missing_deps[@]} -gt 0 ]]; then
    echo -e "ERROR: This script requires the following missing utilities:"
    for utility in ${missing_deps[@]}; do
        echo -e "  ${utility}"
    done
    exit 1
fi

# Parse platform-specific dependencies
for target_platform in ${platform[@]}; do
    # Read platform-specific dependencies
    if [[ -f deployment/${target_platform}/dependencies.txt ]]; then
        platform_dependencies="$( grep -v '^#' deployment/${target_platform}/dependencies.txt )"

        # Verify required utilities are installed
        missing_deps=()
        for utility in ${platform_dependencies[@]}; do
            if ! which ${utility} &>/dev/null; then
                missing_deps+=( ${utility} )
            fi
        done

        # Error if we're missing anything
        if [[ ${#missing_deps[@]} -gt 0 ]]; then
            echo -e "ERROR: The ${target_platform} platform requires the following utilities:"
            for utility in ${missing_deps[@]}; do
                echo -e "  ${utility}"
            done
            exit 1
        fi
    fi
done

# Execute each platform and action in order, if said action is enabled
pushd deployment/
for target_platform in ${platform[@]}; do
    echo -e "> Processing platform ${target_platform}"
    date_start=$( date +%s )
    pushd ${target_platform}
    cleanup() {
        echo -e ">> Processing action clean"
        if [[ -f clean.sh && -x clean.sh ]]; then
            ./clean.sh ${keep_artifacts}
        fi
    }
    trap cleanup EXIT INT
    for target_action in ${action[@]}; do
        echo -e ">> Processing action ${target_action}"
        if [[ -f ${target_action}.sh && -x ${target_action}.sh ]]; then
            ./${target_action}.sh web_branch=${web_branch}
        fi
    done
    if [[ -d pkg-dist/ ]]; then
        echo -e ">> Collecting build artifacts"
        target_dir="../../../bin/${target_platform}"
        mkdir -p ${target_dir}
        mv pkg-dist/* ${target_dir}/
    fi
    cleanup
    date_end=$( date +%s )
    echo -e "> Completed platform ${target_platform} in $( expr ${date_end} - ${date_start} ) seconds."
    popd
done
popd