#!/usr/bin/sh

set -e

DATASET_URL="https://github.com/facebookresearch/ears_dataset/releases/download/dataset"
DATASET_ARCHIVE_PATH="db_archive"
DATASET_PATH="audio_files"

[ ! -d $DATASET_ARCHIVE_PATH ] && mkdir $DATASET_ARCHIVE_PATH

for X in $(seq -w 001 107); do
    FILENAME="p${X}.zip"
    TARGET_FILENAME="${DATASET_ARCHIVE_PATH}/${FILENAME}"
    if [ -f $TARGET_FILENAME ] ; then
        echo "${FILENAME} exists"
    else
        echo "Downloading ${FILENAME}..."
        curl -L "${DATASET_URL}/p${X}.zip" -o $TARGET_FILENAME 
    fi
done

sha512sum --check db_archive.sha512


for X in $(seq -w 001 107); do
    FILENAME="p${X}.zip"
    TARGET_FILENAME="${DATASET_ARCHIVE_PATH}/${FILENAME}"
    if [ -f $TARGET_FILENAME ] ; then
        echo "Extracting ${FILENAME}..."
        unzip -n -qq "${TARGET_FILENAME}" -d "${DATASET_PATH}"
    fi
done

sha512sum --check audio_files.sha512 | grep FAILED

echo "EARS dataset is ready!"
