#!/bin/bash
# support special char ["`$%()[]{},.*+-:;<>?_~/|]

replace_with_value_env(){
  export $(cat .env)
  envsubst < .env > temp
  cat temp > .env
  rm -f temp
}

new_password=$(pwgen -ncCs 14 1)!
sudo sleep 5s
echo $new_password >> /tmp/password.txt
sudo sh -c 'echo "init-password started at" $(date -d now)  1>> /tmp/init_debug.txt'

{% for app_name in applist.stdout_lines %}

if [ ! -d "/data/apps/{{app_name}}" ];then
  continue
else
  dockercompose_up=$(cat /data/apps/{{app_name}}/.env |grep "APP_COMPOSE_UP=false")

  #1 init for applist of .env
  if [ ! $dockercompose_up ]; then
   sudo docker compose -f  /data/apps/{{app_name}}/docker-compose.yml down -v 2>&1 > /dev/null
   sleep 10s
  fi

  # init password
  sudo sed -i "s/POWER_PASSWORD=.*/POWER_PASSWORD=$new_password/g" /data/apps/{{app_name}}/.env
  # has_app_encrypt
  has_app_encrypt=$(cat /data/apps/{{app_name}}/.env |grep "APP_ENCRYPT_PASSWORD")
  if [[ "$has_app_encrypt" =~ "APP_ENCRYPT_PASSWORD" ]]; then
    echo "Encrypt password: $has_app_encrypt" 1>> /tmp/init_debug.txt
    app_encrypt_password=$(bash /data/apps/{{app_name}}/src/encrypt.sh $new_password)
    sudo sed -i "s/APP_ENCRYPT_PASSWORD=.*/APP_ENCRYPT_PASSWORD=$app_encrypt_password/g" /data/apps/{{app_name}}/.env
  fi

  # change all the variables to real value,such as DB_MYSQL_PASSWORD=$APP_PASSWORD=>DB_MYSQL_PASSWORD=ues983ks9309023!
  cd /data/apps/{{app_name}}
  replace_with_value_env
  #while [ `grep -c '\$' .env ` -ne '0' ]
  #do
  #  replace_with_value_env
  #done

  # init APP_URL
  app_url_replace=$(cat /data/apps/{{app_name}}/.env |grep APP_URL_REPLACE)
  if [ $app_url_replace = "APP_URL_REPLACE=true" ]; then
    echo "Change APP_URL" 1>> /tmp/init_debug.txt
    public_ip=`bash /credentials/get_ip.sh`
    urlstr=`grep  "APP_URL=" /data/apps/{{app_name}}/.env |cut -d":" -f1`
    sudo sed -i "s/$urlstr/APP_URL=$public_ip/g" /data/apps/{{app_name}}/.env
  else
    echo "There is not APP_URL"
    public_ip=`bash /credentials/get_ip.sh`
    sudo sed -i "s/appname.example.com/$public_ip/g" /data/apps/{{app_name}}/.env
  fi 

  # delete POWER_PASSWORD
  sudo sed -i "s/POWER_PASSWORD=.*//g" /data/apps/{{app_name}}/.env

  if [ ! $dockercompose_up ]; then
   sudo docker compose -f /data/apps/{{app_name}}/docker-compose.yml up -d  2>&1 > /dev/null
   sleep 10s
  fi

  #2 init for applist of special
  echo "Executing after_up.sh" 1>> /tmp/init_debug.txt
  bash /data/apps/{{app_name}}/src/after_up.sh $new_password 2>&1 > /dev/null

  #3 init for applist of special
  bash /data/apps/{{app_name}}/init_without_docker.sh $new_password 2>&1 > /dev/null

  #4 create password file for guests
  echo "========= credentials for {{app_name}} =========" >> /credentials/password.txt
  cat /data/apps/{{app_name}}/.env | grep 'APP_USER' |awk -F"=" '{print tolower($1)": "$2}' >> /credentials/password.txt
  cat /data/apps/{{app_name}}/.env | grep 'APP_PASSWORD' |awk -F"APP_PASSWORD=" '{print "app_password: "$2}' >> /credentials/password.txt
  cat /data/apps/{{app_name}}/.env | grep -v APP_DB_ | grep APP_|grep PORT |awk -F"=" '{print tolower($1)": "$2}' >> /credentials/password.txt
  cat /data/apps/{{app_name}}/.env | grep 'DB_' |awk -F"=" '{print tolower($1)": "$2}' >> /credentials/password.txt
  sudo sed -i "s/app_/{{app_name}}_/g" /credentials/password.txt
  echo " " >> /credentials/password.txt
fi

{% endfor %}

if [ -d "/data/apps/runtime/src/nginx" ];then
  bash /data/apps/runtime/src/nginx/nginx_init.sh
fi
sudo echo "init docker ended at" $(date -d now)  1>> /tmp/init_debug.txt
