#!/bin/bash
# -*- coding: utf-8; mode: sh; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=sh:et:sw=4:ts=4:sts=4

# Note:
# This script is sourced by the mpbb wrapper script.
# Do not execute this directly!

install-port-usage() {
    # "prog" is defined in mpbb-help.
    # shellcheck disable=SC2154
    cat <<EOF
usage: $prog [<global opts>] install-port [<opts>] <port>

Build and install the given port.

Run \`$prog help' for global options and a list of other subcommands.
EOF
}

get-maintainers() {
    # $option_prefix is set in mpbb
    # shellcheck disable=SC2154
    "${option_prefix}/bin/port" info --index --maintainers --line "$@" | tr ',' '\n' | sort | uniq | tr '\n' ',' | \
        awk '{gsub(/(open|no)maintainer(@macports.org)?,/, ""); print}' | \
        tr '$' '\n' | sed 's/,$//' | tr '@' ';'
}

install-port() {
    local port=${1-}
    if [[ -z $port ]]; then
        err "Must specify a port"
        return 1
    fi
    # $option_log_dir is set in mpbb
    # shellcheck disable=SC2154
    local log_port_contents="${option_log_dir}/port-contents.txt"
    local log_port_stats="${option_log_dir}/port-statistics.txt"
    local log_port_main="${option_log_dir}/main.log"
    local log_subports_progress="${option_log_dir}/ports-progress.txt"

    # prepare the log files and make sure to start with empty ones
    mkdir -p "${option_log_dir}"
    #> "$log_port_contents"
    > "$log_port_stats"

    local time_start
    local time_stop
    time_start=$(date +%s)
    # $option_prefix is set in mpbb
    # shellcheck disable=SC2154
    if "${option_prefix}/bin/port" -dk install "$@"; then
        # Remove failcache if it exists
        failcache_success "$@"
        if [ $? -ne 0 ]; then
            err "failcache_success" "$@" "failed."
            return 1
        fi
    else
        echo "Build of '$port' failed."
        # log: summary for the portwatcher
        echo "Building '$port' ... [ERROR] maintainers: $(get-maintainers "$port")." >> "$log_subports_progress"
        # update failcache
        failcache_failure "$@"
        if [ $? -ne 0 ]; then
            err "failcache_failure" "$@" "failed."
            return 1
        fi
        return 1
    fi
    time_stop=$(date +%s)

    # log: summary for the portwatcher
    echo "Building '$port' ... [OK]" >> "$log_subports_progress"

    # log: contents
    "${option_prefix}/bin/port" -q contents "$port" > "$log_port_contents"

    # TODO: printing statistics (and installing the port + dependencies)
    #       only makes sense when the port hasn't been installed previously
    # log: statistics
    echo "time:    $((time_stop - time_start))s" >> "$log_port_stats"

    local port_workdir
    local port_workdir_size=""
    local port_destdir_size=""
    local print_arg_workdir="ERROR"
    local print_arg_destdir="ERROR"
    # First, compute port_workdir_size and port_destdir_size
    port_workdir=$("${option_prefix}/bin/port" work "$port")
    if [ -n "$port_workdir" ]; then
        port_workdir_size=$(du -ks "$port_workdir" | sed 's/^ *//' | tr '\t' '\n' | head -n 1)
        if [ $? -eq 0 ] && [ -n "$port_workdir_size" ]; then
            print_arg_workdir="${port_workdir_size}k"
        fi

        local port_destdir="$port_workdir/destroot"
        # if we arrive here, 'port work $port' was successful, so we're
        # at least going to print 'destdir: -'
        print_arg_destdir="-"
        if [ -d "$port_destdir" ]; then
            port_destdir_size=$(du -ks "$port_destdir" | sed 's/^ *//' | tr '\t' '\n' | head -n 1)
            if [ $? -eq 0 ] && [ -n "$port_destdir_size" ]; then
                print_arg_destdir="${port_destdir_size}k"
            fi
        fi
    fi
    # Then print them, or on error (or if destdir doesn't exist), print the
    # appropriate message
    echo "workdir: $print_arg_workdir" >> "$log_port_stats"
    echo "destdir: $print_arg_destdir" >> "$log_port_stats"

    # log: main.log
    local port_mainlog
    port_mainlog=$("${option_prefix}/bin/port" logfile "$port")
    if [ $? -eq 0 ] && [ -f "$port_mainlog" ]; then
        cp -f "$port_mainlog" "$log_port_main"
    fi
}