#!/usr/bin/bash

# Backup directory name to prevent overwriting.
DATETIME=`date +%Y%m%d_%H%M%S_backup`

# Variables and initial values for setting the mode of this script.
# (set based on arguments and options passed to this script)
DOWNLOAD_MODE="default"
EXTRACTION_MODE="default"
BUILD_MODE="default"

function help() {
    echo "usage: $0 <options>"
    echo "<options>:"
    echo "    -d | --download-mode [\"default\"|\"force\"]"
    echo "        \"default\": Skips the download process"
    echo "                   if the file has already been downloaded."
    echo "        \"force\"  : Rerun the download process"
    echo "                   even if the file has already been downloaded."
    echo "    -e | --extraction-mode [\"default\"|\"force\"]"
    echo "        \"default\": Skips the extraction process"
    echo "                   if the file has already been extracted."
    echo "        \"force\"  : Rerun the extraction process"
    echo "                   even if the file has already been extracted."
    echo "    -b | --build-mode [\"default\"|\"rebuild\"|\"skip\"|\"force\"]"
    echo "        \"default\": Retry the make process (Run 'make clean && make'.)"
    echo "                   even if the sample program has already been built."
    echo "        \"rebuild\": Same meaning as the \"default\"."
    echo "        \"skip\"   : Skips the build process"
    echo "                   if the sample program has already been built."
    echo "        \"force\"  : Rerun the build process (Recreate the build dir.)"
    echo "                   even if the sample program has already been built."
    echo "    -h | --help"
    echo "    --force-download"
    echo "        Same meaning as the '--download-mode \"force\"' option."
    echo "    --force-extraction"
    echo "        Same meaning as the '--extraction-mode \"force\"' option."
    echo "    --force-build"
    echo "        Same meaning as the '--build-mode \"force\"' option."
}

function download_samples() {
    if [ "$1" == "force" ] && [ -f ./HARK_recog_3.0.0_practice2.zip ]; then
        mkdir -p ./${DATETIME}
        mv ./HARK_recog_3.0.0_practice2.zip ./${DATETIME}/
    fi
    if [ ! -f ./HARK_recog_3.0.0_practice2.zip ]; then
        wget https://hark.jp/networks/HARK_recog_3.0.0_practice2.zip
    fi
}

function extract_samples() {
    if [ "$1" == "force" ] && [ -d ./HARK_recog_3.0.0_practice2 ]; then
        mkdir -p ./${DATETIME}
        mv ./HARK_recog_3.0.0_practice2 ./${DATETIME}/
    fi
    if [ ! -d ./HARK_recog_3.0.0_practice2 ]; then
        unzip ./HARK_recog_3.0.0_practice2.zip
    fi
}

function build_cpp_binary() {
    if [ "$1" == "force" ] && [ -d ../build ]; then
        mkdir -p ../${DATETIME}
        mv ../build ../${DATETIME}/
    fi
    if [ ! -d ../build ]; then
        (cd .. && mkdir build && cd build && cmake .. && make)
    elif [ "$1" == "default" ] || [ "$1" == "rebuild" ]; then
        (cd .. && cd build && cmake .. && make clean && make)
    fi
    if [ ! -f ../build/src/hark-lib-sample-tutorial ]; then
        echo "[Error] We built \"hark-lib-sample-tutorial\" but could not find \"${PWD}/../build/src/hark-lib-sample-tutorial\" ."
        exit
    fi
}

#### argument parser
while true; do
    case "$1" in
        "-d" | "--download-mode") DOWNLAOD_MODE="$2"; shift 2 ;;
        "-e" | "--extraction-mode") EXTRACTION_MODE="$2"; shift 2 ;;
        "-b" | "--build-mode") BUILD_MODE="$2"; shift 2 ;;
        "-h" | "--help") help ; shift ; exit ;;
        "--force-download") DOWNLOAD_MODE="force"; shift ;;
        "--force-extraction") EXTRACTION_MODE="force"; shift ;;
        "--force-build") BUILD_MODE="force"; shift ;;
        "--") shift ; break ;;
        *) break ;;
    esac
done

#### check
if [ ! -d ./assets ]; then
    echo "[Error] We could not find \"assets\" directory."
    exit
fi

#### prepare
download_samples ${DOWNLOAD_MODE}
extract_samples ${EXTRACTION_MODE}
build_cpp_binary ${BUILD_MODE}

#### construct
if [ ! -d ./build ]; then
    mkdir -p build
fi

cp -a ./assets/* ./build/

cp -a ./HARK_recog_3.0.0_practice2/2SPK-ja.wav ./build/
cp -a ./HARK_recog_3.0.0_practice2/hark_conf/tamago_rectf.zip ./build/
cp -ra ./HARK_recog_3.0.0_practice2/kaldi_conf ./build/
cp -ra ./HARK_recog_3.0.0_practice2/python ./build/
cp -a ./HARK_recog_3.0.0_practice2/transcription_*.txt ./build/

cp -a ../build/src/hark-lib-sample-tutorial ./build/

cp -a ../python/tutorial.py ./build/
cp -a ../python/tutorial_flat.py ./build/

