#! /bin/bash

# Use sudo?
sudo=$(which sudo)
set -eux

# Build SoapySDR
if ! [ -d SoapySDR ]; then
git clone https://github.com/pothosware/SoapySDR.git
pushd SoapySDR
    mkdir build
    pushd build
        cmake .. -DCMAKE_BUILD_TYPE=Release
        make -j
        $sudo make install
        $sudo ldconfig
        SoapySDRUtil --info #test SoapySDR install
    popd
popd
fi

# Build SoapySDRPlay plugin
if ! [ -d SoapySDRPlay ]; then
git clone https://github.com/pothosware/SoapySDRPlay.git
pushd SoapySDRPlay
    mkdir build
    pushd build
        cmake .. -DCMAKE_BUILD_TYPE=Release
        make -j
        $sudo make install
    popd
popd
fi

# Build SoapyRTLSDR plugin
if ! [ -d SoapyRTLSDR ]; then
git clone https://github.com/pothosware/SoapyRTLSDR.git
pushd SoapyRTLSDR
    mkdir build
    pushd build
        cmake .. -DCMAKE_BUILD_TYPE=Release
        make -j
        $sudo make install
    popd
popd
fi

# SoapySDRPlay plugin can overwrite some of the installed files for SoapySDR with older
# versions - re-run the installer for SoapySDR base to ensure we have the latest ones.
pushd SoapySDR/build
    $sudo make install
popd