#!/bin/bash
set -e
librarydir() { python3 -c "from openmapflow.constants import LIBRARY_DIR; print(LIBRARY_DIR)"; }
version() { python3 -c "from openmapflow.constants import VERSION; print(VERSION)"; }
datasets() { python3 -c "from openmapflow.config import DataPaths; print(DataPaths.REPORT)"; }
check_openmapflow_yaml() {
    if [ ! -f "openmapflow.yaml" ]; then
        echo "No openmapflow.yaml file found. Please create one by running: openmapflow generate"
        return 1
    fi
}


case $1 in
    "cp")
        cp -r "$(librarydir)"/"${@: -2:1}" "${@: -1:1}"
        ;;
    "create-datasets")
        check_openmapflow_yaml
        python3 -c "from datasets import datasets; from openmapflow.labeled_dataset import create_datasets; create_datasets(datasets)"
        ;;
    "datapath")
        check_openmapflow_yaml
        python3 -c "from openmapflow.config import DataPaths; print(DataPaths.get('$2'))"
        ;;
    "datasets")
        check_openmapflow_yaml
        cat "$(datasets)"
        ;;
    "deploy")
        check_openmapflow_yaml
        source deploy.sh
        ;;
    "dir")
        librarydir
        ;;
    "generate")
        python3 "$(librarydir)/generate.py"
        ;;
    "ls")
        ls "$(librarydir)/$2"
        ;;
    "version")
        version
        ;;
    "--version")
        version
        ;;
    *)
        echo "---------------------------------------------------------------------------------"
        echo "                              OpenMapFlow CLI" 
        echo "---------------------------------------------------------------------------------"
        echo "openmapflow cp <source> <destination> - copy a file or directory from the library"
        echo "openmapflow create-datasets - creates datasets for all datasets in datasets.py"
        echo "openmapflow datapath <DATAPATH> - outputs a relative path to the data directory"
        echo "openmapflow datasets - outputs a list of all datasets"
        echo "openmapflow deploy - deploys Google Cloud Architecture for project"
        echo "openmapflow dir - outputs openmapflow library directory"
        echo "openmapflow generate - generates an openmapflow project"
        echo "openmapflow help - outputs this message"
        echo "openmapflow ls - lists files in openmapflow library directory"
        echo "openmapflow version - package version"
        ;;
    esac
