#!/bin/bash

### Init variables ###
## Directories
local_colibri_root="/opt/colibri"
local_dp_folder="$local_colibri_root/dp/"
local_docker_compose_folder="$local_dp_folder/docker/"
local_automount_folder="$local_colibri_root/automount"
local_automount_images_folder="$local_automount_folder/img"
local_automount_reports_folder="$local_automount_folder/report"
local_repo_folder="$local_colibri_root/repositories"
local_software_folder="$local_repo_folder/software"
local_alt_repo_folder="$local_software_folder/alt_repo"
local_deb_repo_folder="$local_software_folder/deb_repo"
local_rpm_repo_folder="$local_software_folder/rpm_repo"
local_cron_folder="/etc/cron.d"

## Files
local_automount_cron_task="$local_cron_folder/colibri_osimage_mount"
local_automount_script="$local_automount_folder/automount.sh"
local_dp_env_file="$local_docker_compose_folder/.env"
local_dp0_env_file="$local_dp_folder/.env"

## Containers
container_dp_nginx="colibri_dp_nginx"
container_dp0_nginx="nginx"
container_dp_samba="colibri_dp_samba"
container_dp0_samba="samba"

## Error counter
err_count=0
### End Init variables

### Custom functions
StdErr () {
    >&2 echo "$1"
}

StdOut () {
    echo "$1"
}
### End Custom functions

### Test repository folders
if [ ! -d "$local_alt_repo_folder" ]; then
    StdErr "Alt repo folder - not detected"
    err_count=$((err_count+1))
else
    StdOut "Alt repo folder - detected"
fi

if [ ! -d "$local_deb_repo_folder" ]; then
    StdErr "Deb repo folder - not detected"
    err_count=$((err_count+1))
else
    StdOut "Deb repo folder - detected"
fi

if [ ! -d "$local_rpm_repo_folder" ]; then
    StdErr "Rpm repo folder - not detected"
    err_count=$((err_count+1))
else
    StdOut "Rpm repo folder - detected"
fi

### Test automount resources
if [ ! -f "$local_automount_script" ]; then
    StdErr "Automount script - not detected"
    err_count=$((err_count+1))
else
    StdOut "Automount script - detected"
fi

if [ ! -d "$local_automount_reports_folder" ]; then
    StdErr "Automount reports - not detected"
    err_count=$((err_count+1))
else
    StdOut "Automount reports - detected"
fi

if [ ! -d "$local_automount_images_folder" ]; then
    StdErr "Automount images - not detected"
    err_count=$((err_count+1))
else
    StdOut "Automount images - detected"
fi

if [ ! -f "$local_automount_cron_task" ]; then
    StdErr "Automount cron task - not detected"
    err_count=$((err_count+1))
else
    StdOut "Automount cron task - detected"
fi

### Read environment file
if [ -f $local_dp_env_file ]; then
    container_nginx=$container_dp_nginx
    container_samba=$container_dp_samba
    eval "$(cat $local_dp_env_file)"
fi

if [ -f $local_dp0_env_file ]; then
    container_nginx=$container_dp0_nginx
    container_samba=$container_dp0_samba
    eval "$(cat $local_dp0_env_file)"
fi

### Test containers
## Nginx
if grep -q "$container_nginx" <<< "$(docker ps)"; then
    StdOut "Container for nginx - detected"
    status=$(docker container inspect -f '{{.State.Running}}' "$container_nginx" 2>/dev/null)
    if [ "$status" == "true" ]; then
        StdOut "Container for nginx - started"
    else
        StdErr "Container for nginx - $status"
        err_count=$((err_count+1))
    fi
    health=$(docker container inspect -f '{{.State.Health.Status}}' "$container_nginx" 2>/dev/null)
    if [ "$health" == "healthy" ]; then
        StdOut "Container for nginx - healthy"
    else
        StdErr "Container for nginx - $health"
        err_count=$((err_count+1))
    fi
else
    StdErr "Container for nginx - not detected"
    err_count=$((err_count+1))
fi

## Samba
if grep -q "$container_samba" <<< "$(docker ps)"; then
    StdOut "Container for samba - detected"
    status=$(docker container inspect -f '{{.State.Running}}' "$container_samba" 2>/dev/null)
    if [ "$status" == "true" ]; then
        StdOut "Container for samba - started"
    else
        StdErr "Container for samba - $status"
        err_count=$((err_count+1))
    fi
    health=$(docker container inspect -f '{{.State.Health.Status}}' "$container_samba" 2>/dev/null)
    if [ "$health" == "healthy" ]; then
        StdOut "Container for samba - healthy"
    else
        StdErr "Container for samba - $health"
        err_count=$((err_count+1))
    fi
else
    StdErr "Container for samba - not detected"
    err_count=$((err_count+1))
fi

### Test services ports
if [ -n "$NGINX_DP_PORT" ]; then
    ## Test netstat
    if [ -z "$(which netstat)" ]; then
        StdOut "Skip port test [netstat] is not detected"
    else
        ## Test Nginx
        # shellcheck disable=SC2143
        if [ -z "$(netstat -tulpn | grep LISTEN | grep ":${NGINX_DP_PORT} " | grep nginx)" ]; then
            StdErr "Nginx port is not listened"
            err_count=$((err_count+1))
        else
            StdOut "Nginx port is listening"
        fi
        ## Test Samba
        # shellcheck disable=SC2143
        if [ -z "$(netstat -tulpn | grep LISTEN | grep ':445 ' | grep smbd)" ]; then
            StdErr "Samba port is not listened"
            err_count=$((err_count+1))
        else
            StdOut "Samba port is listening"
        fi
    fi
else 
    StdErr "DP environment file not found"
    err_count=$((err_count+1))
fi

exit $err_count