#!/bin/bash

# Load .env file if it exists
if [ -f .env ]
then
  export $(cat .env | sed 's/#.*//g' | xargs)
fi

cmd_create_database() {
    echo "Creating right_tree database..."
    docker-compose down --remove-orphans --volumes
    docker-compose -f docker-compose.yaml up postgres | sed '/PostgreSQL init process complete; ready for start up./q'
    docker-compose down
}

cmd_makemigrations() {
    echo "Creating database migrations..."
    docker-compose exec backend python manage.py makemigrations --no-input
}

cmd_migrate() {
    echo "Running database migrations..."
    docker-compose exec backend python manage.py migrate
}

cmd_createsuperuser() {
    echo "Creating django superuser..."
    docker-compose run backend python manage.py createsuperuser
}

cmd_load_fixtures() {
    echo "Loading fixtures..."
    docker-compose exec backend bash -c "python manage.py loaddata right_tree/api/data/fixtures/*.json"
}

cmd_load_shapefiles() {
    echo "Loading shapefiles into the database..."
    docker-compose exec backend python manage.py loadshapefiles
}

cmd_create_plant_fixtures() {
    echo "Creates fixtures for plants using spreadsheet."
    docker-compose exec backend python manage.py createplantfixtures
}

cmd_reset_plants() {
    echo "Resetting plants..."
    docker-compose exec backend python manage.py resetplants
}

cmd_load_plant_fixtures() {
    echo "Loading plants..."
    docker-compose exec backend python manage.py loaddata right_tree/api/data/fixtures/plants.json
}

cmd_load_plants() {
    cmd_create_plant_fixtures
    cmd_reset_plants
    cmd_load_plant_fixtures    
}

cmd_load_sites_from_spreadsheet() {
    echo "Loading habitats and zones..."
    docker-compose exec backend python manage.py loadsitedata
}

cmd_populate_database() {
    echo "Populating the database..."
    docker-compose up -d backend postgres

    cmd_makemigrations
    cmd_migrate
    cmd_load_fixtures
    cmd_load_shapefiles
    cmd_load_plants

    docker-compose down
}

cmd_init_database() {
    cmd_create_database
    cmd_populate_database
}

cmd_reset_database() {
    cmd_init_database
}

cmd_build() {
    docker-compose build
}

cmd_start() {
    docker-compose up --remove-orphans
}

cmd_collectstatic() {
    docker-compose -f docker-compose.production.yaml build
    docker-compose -f docker-compose.production.yaml run backend python manage.py collectstatic --no-input
}

cmd_build_frontend() {
    docker run -v $PWD/frontend:/app -w /app node:16-alpine3.11 npm install
    docker run -v $PWD/frontend:/app -w /app node:16-alpine3.11 mkdir -p node_modules/.cache
    docker run -v $PWD/frontend:/app -w /app node:16-alpine3.11 chmod -R 777 node_modules/.cache
    docker run -v $PWD/frontend:/app -w /app node:16-alpine3.11 npm run build
}

cmd_create_staticfiles() {
    cmd_collectstatic
    cmd_build_frontend
}

cmd_build_production() {
    docker-compose -f docker-compose.production.yaml build
}

cmd_start_production() {
    docker-compose -f docker-compose.production.yaml up  --remove-orphans
}

cmd_stop_production() {
    docker-compose -f docker-compose.production.yaml stop  --remove-orphans
}

cmd_stop_production() {
    docker-compose -f docker-compose.production.yaml stop  --remove-orphans
}

cmd_renew_certifcate() {
    cmd_stop_production
    sudo docker run -i --rm --name certbot -p 443:443 -p 80:80 -v /etc/letsencrypt:/etc/letsencrypt/ certbot/certbot renew --dry-run -d $BASE_URL --logs-dir /etc/letsencrypt/logs
    cmd_start_production
}

# Run the command
cmd="$1"
"cmd_$cmd" "$@"