#!/bin/bash

base_path="/var/www/html"
#base_path="/opt"

application_config_file_path="protected/config/env.php"
#application_config_file_path="chrome_100_percent.pak"

target_file="/var/www/html/index.html"
#target_file="/home/neftaio/Develop/Dinamicacd/apiprocess-home/src/index.html"

target_name="data-access-links"

server_url="http://52.40.134.248"

echo "Environments Synchronization . . ."

paths=(ls -d $base_path/*/)

applications=()

for appdir in "${paths[@]}"
do
    if [ -f "${appdir}${application_config_file_path}" ]; then
        applications+=($appdir)
    fi
done

applicationsJson="{\"apps\":[{\"name\": \"Data Base\", \"url\": \"${server_url}/phpmyadmin\", \"date\": \"UND.\", \"icon\": \"fa-chart-pie\"}"

for apps in "${applications[@]}"
do
    name=`basename $apps`

    url="${server_url}/${name}"

    date=`stat -c %y ${apps} | awk '{print $1}'`

    applicationsJson=${applicationsJson}", {\"name\": \"${name}\", \"url\": \"${url}\", \"date\": \"${date}\", \"icon\": \"fa-chart-line\"}"
done

applicationsJson=${applicationsJson}"]}"

meta_data="<meta name=\"data-access-links\" content='${applicationsJson}' />"

#echo "The Meta Data:"
#echo $meta_data
#read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1

sed -i  --expression "s@.*<meta name=\"data-access-links\" content='.*' />@${meta_data}@" $target_file

echo "End :D"
