#!/bin/bash
#
# workspace-autodetect Autodetects shared workspace directories
#
# Copyright (C) 2020  Open Mobile Platform LLC.
# Contact: http://jolla.com/
# All rights reserved.
#
# You may use this file under the terms of BSD license as follows:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the Jolla Ltd nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


set -o nounset
set -o pipefail

usage()
{
    cat <<END
usage: workspace-autodetect [--dry-run] [--verbose]

Starts systemd services for mounting workspace directories shared from host.
END
}

_()
{
    if [[ $OPT_VERBOSE ]]; then
        printf '+'
        printf ' %q' "$@"
        printf '\n'
    fi >&2

    [[ $OPT_DRY_RUN ]] || "$@"
}

start()
{
    local idx=$1

    local templates=$(systemctl list-unit-files |sed -n 's/^\(workspace-.*@.service\)\s.*/\1/p')
    _ systemctl start ${templates//@/@$idx}
}

detect()
{
    for ((i=1; ; i++)); do
        eval local mount_point=\${SAILFISH_SDK_SRC${i}_MOUNT_POINT:-}
        if [[ $mount_point ]]; then
            printf '%d %s\n' "$i" "$mount_point"
        else
            break
        fi
    done
}

parse_opts()
{
    OPT_HELP=
    OPT_DRY_RUN=
    OPT_VERBOSE=

    while [[ $# -gt 0 ]]; do
        case $1 in
            -h|--help)
                OPT_HELP=1
                return
                ;;
            --dry-run)
                OPT_DRY_RUN=1
                ;;
            --verbose)
                OPT_VERBOSE=1
                ;;
            *)
                echo "Unexpected argument: $1" >&2
                return 1
                ;;
        esac
        shift
    done
}

main()
{
    parse_opts "$@" || return

    if [[ $OPT_HELP ]]; then
        usage
        return 0
    fi

    local detected=
    detected=$(detect) || return

    if [[ ! $detected ]]; then
        echo "No workspace detected" >&2
        return 1
    fi

    local rc=0

    local idx= mount_point=
    while read idx mount_point; do
        # This output is processed by sdk-info!
        echo "Adding workspace $mount_point" >&2
        start "$idx" || rc=1
    done <<<"$detected"

    return $rc
}

main "$@"
