#!/bin/bash

# UTU GeoNode customisation script
#
# Usually this file does not need to be executed manually.
# 
# Execute update.sh that will pull latest changes from git
# and execute this script afterwards.
#
# If you modify files list please keep the custom css file
# first or file verification check might not give planned
# results. New files should be added at the bottom of the
# lists.
#
# This file is supposed to be more readable and simple
# than efficient and smart. Please keep it that way.
#
# softdev@utu.fi

# Directory to store the backups
BACKUPS='backups'

# Temporary direction for backup creation
TMP='tmp'

# Image files path
IMAGES='images'

# Image path inside container
CONTAINER_IMAGE_PATH='/usr/src/resilienceacademy/resilienceacademy/static/img'

# File names inside container
CONTAINER_FILES=(
    'geonode-utu.css'      #  0 (does not exist inside container on first run)
    'site_base.html'       #  1
    'index.html'           #  2
    'base.html'            #  3
    'urls.py'              #  4
    'training.html'        #  5
    'guide.html'           #  6
    'events.html'          #  7
    'utu_geonode.html'     #  8
    'influence.html'       #  9
    'contact.html'         # 10
    'blog.html'            # 11
    'utu-blog.js'          # 12
    )
   
# File paths inside container
CONTAINER_PATHS=(
    '/usr/src/resilienceacademy/resilienceacademy/static/css/'                #  0
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  1
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  2
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  3
    '/usr/local/lib/python2.7/site-packages/geonode/'                         #  4
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  5
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  6
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  7
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  8
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 #  9
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 # 10
    '/usr/src/resilienceacademy/resilienceacademy/templates/'                 # 11
    '/usr/src/resilienceacademy/resilienceacademy/static/js/'                 # 12
    )
  
# File names outside container (here)
# This is needed because there are files with same name on different path
LOCAL_FILES=(
    'geonode-utu.css'      #  0
    'site_base.html'       #  1
    'index.html'           #  2
    'base.html'            #  3
    'urls.py'              #  4
    'training.html'        #  5
    'guide.html'           #  6
    'events.html'          #  7
    'utu_geonode.html'     #  8
    'influence.html'       #  9
    'contact.html'         # 10
    'blog.html'            # 11
    'utu-blog.js'          # 12
  )

# Image files, these are not backed up but existence is checked
IMAGE_FILES=(
    'UTU_logo_white.png'
    'banner-2000x700px.jpg'
  )

# Get django container id
CONTAINER=$(docker ps -aqf "name=django4resilienceacademy")

# Exit if django container is not found
if [ -z "$CONTAINER" ]; then
    echo "No django container found. Halting script exection."
    exit 1
fi

# Create backups directory if it does not exist
if [ ! -d "$BACKUPS" ]; then
    mkdir "$BACKUPS"
fi

# Remove temp directory if it exists already
if [ -d "$TMP" ]; then
    echo "Temp directory $TMP already exists. Removing it before creating a new one."
    rm -rf "$TMP"
fi

# Create the temp directory
mkdir "$TMP"

# Copy all container files to local temp and ensure that file exists (exit if not)
for ((i = 0; i < ${#CONTAINER_FILES[@]};++i)); do
  
    # Copy file to temp directory
    docker cp $CONTAINER:${CONTAINER_PATHS[$i]}${CONTAINER_FILES[$i]} $TMP/${LOCAL_FILES[$i]}

    # 2019-12-16 olljal: Removed file existing check from here - new files added
  
done

# Create a zip file from backups
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
zip -rq "$BACKUPS/utu-originals-$TIMESTAMP.zip" "$TMP"

# Check that the zip file is found
if [ ! -s "$BACKUPS/utu-originals-$TIMESTAMP.zip" ]; then
    echo "Created backup $BACKUPS/utu-originals-$TIMESTAMP.zip is missing! Halting script exection.."
    exit 1
fi

# Check that every customisation file exists locally and is not empty
for ((i = 0; i < ${#CONTAINER_FILES[@]};++i)); do
    if [ ! -s "${LOCAL_FILES[$i]}" ]; then
        echo "Required customisation file ${LOCAL_FILES[$i]} is empty or does not exist! Halting script exection."
        exit 1
    fi
done

# Check that every image file exists locally and is not empty
for ((i = 0; i < ${#IMAGE_FILES[@]};++i)); do
    if [ ! -s "$IMAGES/${IMAGE_FILES[$i]}" ]; then
        echo "Required image file ${IMAGE_FILES[$i]} is empty or does not exist! Halting script exection."
        exit 1
    fi
done

# Update container files
for ((i = 0; i < ${#CONTAINER_FILES[@]};++i)); do
    # Copy file to container
    docker cp ${LOCAL_FILES[$i]} $CONTAINER:${CONTAINER_PATHS[$i]}${CONTAINER_FILES[$i]}
done

# Update container images
for ((i = 0; i < ${#IMAGE_FILES[@]};++i)); do
    # Copy image to container
    docker cp $IMAGES/${IMAGE_FILES[$i]} $CONTAINER:${CONTAINER_IMAGE_PATH}/${IMAGE_FILES[$i]}
done

# Remove temp directory
rm -rf "$TMP"

# Run 'collectstatic' management command
docker exec -it $CONTAINER bash -c "/usr/src/resilienceacademy/manage.sh collectstatic --noinput"

# Restart django container to show customisation (test without)
# echo "Restarting django container, this will take some seconds"
# docker restart "$CONTAINER"

# Tell user that customisation is up to date
echo "Customisation update ready."

echo "Restart the django container if updates are not showing up:"
echo "docker restart \"$CONTAINER\""