DIRECTORY=`dirname $0`

#MYSQL_ROOT_PASSWORD=$(grep MYSQL_ROOT_PASSWORD $DIRECTORY"/.env" | xargs)
#MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD#*=}
source $DIRECTORY"/.env"
help() {
    echo "run $0 [COMMAND] "
    echo '
  "init"            - run init flow 
  "up"              - up -d all services 
  "down"            - down all services without volumes 
  "composer"        - run composer ${@:2}
  "psalm"           - run composer analyse
  "php-fixer"       - run composer cs
  "deptrac"         - run composer deptrac
  "tests"           - run composer tests
  "console"         - run docker compose exec php ./bin/console ${@:2}
  "db"              - run mysql shell 
  "db-create"       - crate database 
  "backup-dev"      -  
  "backup-create"   -
  "backup-exec"     -
  "restore-dev"     -
  "purge-db"        - purge database
  "restore-db"      - restore database from s3 backup
  "front"           - exec front container

'
}

log() {
    RED='\e[31m'
    GREEN='\e[32m'

    case $2 in
        info)
            printf "${GREEN}${1}\e[0m\n"
        ;;
        error)
            printf "${RED}${1}\e[0m\n"
        ;;
        *)
            printf "${1}"
        ;;
    esac
}

## restore last backup from s3 storage 
restoredb() {
    log "run restore database"
    sudo rclone copy --verbose selectel:backup_db/dot/prod/dot.$(date +%a).sql.gz backups/
    zcat backups/dot.$(date +%a).sql.gz | docker compose exec -T db mysql -uroot -p${MYSQL_ROOT_PASSWORD} dot 
}

checkDockerCompose() {
  if ! [ -f "docker-compose.yml" ]; then
    log "docker-compose.yml not exist. Run init command" "error"
    exit
  fi
}

init() {
  dumpfile=$(ls backups/ | sort | tail -1)
  if ! [ -f backups/${dumpfile} ]; then
    log "put dot.sql to backups directory" "error"
    exit
  fi

  if ! [ -f ".env" ]; then
    log ".env not exist. Run init command" "error"
    exit
  fi

  up

  docker compose exec php composer install
#  docker compose exec front npm i

#  docker compose exec front npm run build

  if ! docker compose exec db mysql -uroot -proot -e "show databases" | grep "dot "; then
    createDatabase "dot"
    execBackup "dot" ${dumpfile}
  fi
}

up() {
  checkDockerCompose
  docker compose up -d
}

down() {
  checkDockerCompose
  docker compose down
}

createDatabase() {
  docker compose exec db mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create database $1"
  log "create database $1" "info"
}

createBackup() {
  docker compose exec db mysqldump -uroot -p${MYSQL_ROOT_PASSWORD} $1 > backups/$2.sql
}

createBackupWithExceptions() {
  docker compose exec db mysqldump -uroot -p${MYSQL_ROOT_PASSWORD} --ignore-table=dot.ml_calculator_log dot > backups/dot_develop.sql
  docker compose exec db mysqldump -uroot -p${MYSQL_ROOT_PASSWORD} --no-data dot ml_calculator_log >> backups/dot_develop.sql
}

execBackup() {
   zcat backups/$2 | docker compose exec -T db mysql -uroot -p${MYSQL_ROOT_PASSWORD} $1
   log "exec $2.sql backup" "info"
}

execDevBackup() {
  docker compose exec -T db mysql -uroot -p${MYSQL_ROOT_PASSWORD} dot < backups/dot_develop.sql
      log "exec dot_develop.sql backup" "info"
}

purgeDb() {
  docker compose exec db mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "drop database dot"
    log "dropped database dot" "info"
  docker compose exec db mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create database dot"
    log "created database dot" "info"
}

composer() {
  docker compose exec php php -d memory_limit=-1 /usr/local/bin/composer ${@:1}
}

case "$1" in
  "help")
    help;;
  "init")
    init;;
  "up")
    up;;
  "down")
    down;;
  "composer"*)
    composer ${@:2};;
  "psalm"*)
    composer analyse;;
  "php-fixer")
    composer cs;;
  "deptrac")
    composer deptrac;;
	"tests")
		composer tests;;
  "console"*)
    docker compose exec  --user www-data php ./bin/console ${@:2};;
  "db")
    docker compose exec db mysql -uroot -p${MYSQL_ROOT_PASSWORD};;
  "db-create")
    createDatabase $2;;
  "backup-dev"*)
    createBackupWithExceptions $2;;
  "backup-create"*)
    createBackup $2 $3;;
  "backup-exec"*)
    execBackup $2 $3;;
  "restore-dev"*)
      execDevBackup;;
  "purge-db"*)
      purgeDb;;
  "restore-db")
      restoredb;;
  "front"*)
    case $2 in
      *)
        docker compose exec landing-ttl ${@:2};;
    esac;;
  *)
    help
    #docker compose exec php ./bin/console ${@:1};;
esac
