#!/bin/bash

target=$(dirname $0)

if [ -n "$1" ]; then
    case "$1" in
        -h|--help)
        echo "Usage: $0 [target_dir]"
        exit 0
        ;;
    esac
    if ! [ -d "$1" ]; then
        echo "$1 not a valid directory. Exiting"
        exit 1
    fi
    target=$1
fi

echo "Recording 10 seconds of noise. Please, be quiet :)"
read -n 1 -p "Press y when ready " yn
echo
if [ "$yn" != "y" ]; then
    echo "Aborted by user request"
    return
fi
echo "Recording..."
sox -q -v 10 -r 44100 -2 -c 1 -t ossdsp /dev/dsp $target/noise.flac trim 0 10
echo "Building noise profile..."
#play $target/noise.flac noiseprof $target/noise_profile.sox
sox $target/noise.flac -n noiseprof $target/noise_profile.sox
echo "Noise profile build and stored in $target/noise_profile.sox"