#!/bin/bash
function check_vehicle_software_prereqs {

    if [ "$IS_LOCAL" = "true" ]; then
        if ! test -f "$VEHICLE_MKDIR"; then
            echo "'mkdir' was not found on the vehicle at: $VEHICLE_MKDIR"
            exit 1
        fi
        if ! test -f "$VEHICLE_RM"; then
            echo "'rm' was not found on the vehicle at: $VEHICLE_RM"
            exit 1
        fi
        if ! test -f "$VEHICLE_CAT"; then
            echo "'cat' was not found on the vehicle at: $VEHICLE_CAT"
            exit 1
        fi
        if ! test -f "$VEHICLE_CP"; then
            echo "'cp' was not found on the vehicle at: $VEHICLE_CP"
            exit 1
        fi
        if ! test -f "$VEHICLE_WC"; then
            echo "'wc' was not found on the vehicle at: $VEHICLE_WC"
            exit 1
        fi
        if ! test -f "$VEHICLE_JQ"; then
            echo "'jq' was not found on the vehicle at: $VEHICLE_JQ"
            exit 1
        fi
        if ! test -f "$VEHICLE_CURL"; then
            echo "'curl' was not found on the vehicle at: $VEHICLE_CURL"
            exit 1
        fi
        if ! test -f "$VEHICLE_OPENSSL"; then
            echo "openssl binary was not found on the vehicle at: $VEHICLE_OPENSSL"
            exit 1
        fi
    else
        if [ "$IS_LOCAL" = "false" ]; then
            if ! test -f "$OP_SSH"; then
                echo "'ssh' was not found on this operator machine at $OP_SSH ."
                exit 1
            fi
            if [ -z "$VEHICLE_USER" ]; then
                echo "No vehicle user provided for a non-local run."
                exit 1
            fi
            if [ -z "$VEHICLE_ADDR" ]; then
                echo "No vehicle address provided for a non-local run."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_MKDIR"; then
                echo "'mkdir' was not found on the vehicle at: $VEHICLE_MKDIR . check its accessibility over SSH. Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_RM"; then
                echo "'rm' was not found on the vehicle at: $VEHICLE_RM . check its accessibility over SSH. Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_CAT"; then
                echo "'cat' was not found on the vehicle at: $VEHICLE_CAT . check its accessibility over SSH. Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_CP"; then
                echo "'cp' was not found on the vehicle at: $VEHICLE_CP . check its accessibility over SSH. Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_WC"; then
                echo "'wc' was not found on the vehicle at: $VEHICLE_WC . check its accessibility over SSH. Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_JQ"; then
                echo "'jq' was not found on the vehicle at: $VEHICLE_JQ . check its accessibility over SSH. Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_CURL"; then
                echo "'curl' was not found on the vehicle at: $VEHICLE_CURL . check its accessibility over SSH. Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
            if ! "$OP_SSH" -q "$VEHICLE_USER@$VEHICLE_ADDR" "test -f $VEHICLE_OPENSSL"; then
                echo "openssl binary was not found on the vehicle at: $VEHICLE_OPENSSL . Check its accessibility over SSH (at $OP_SSH) for user $VEHICLE_USER and vehicle $VEHICLE_ADDR."
                exit 1
            fi
        else
            echo "Invalid value for '-l' or '--local' option. Legitimate values are: 'true' or 'false'; case-sensitive and without single-quotes."
            exit 1
        fi
    fi
}

function check_provisioner_software_prereqs {
        if ! test -f "$OP_SSH"; then
            echo "'ssh' was not found on this operator machine at $OP_SSH ."
            exit 1
        fi
        if ! test -f "$OP_JQ"; then
            echo "'jq' was not found on the provisioner at: $OP_JQ"
            exit 1
        fi
        if ! test -f "$OP_CURL"; then
            echo "'curl' was not found on the provisioner at: $OP_CURL"
            exit 1
        fi
}
