container_commands:
    01_chmod:
        command: "chmod 0755 ebscripts/*.sh"
    02_copy_configuration:
        command: "ebscripts/copy_configuration.sh"
    03_composer_install:
        command: "USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user) && sudo -u ${USER} -H ebscripts/composer_install.sh"
    04_update_htaccess:
        command: "USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user) && sudo -u ${USER} -H ebscripts/update_htaccess.sh"
    05_doctrine_database_create_execute:
        command: "USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user) && sudo -u ${USER} -H ebscripts/doctrine_database_create.sh"
        leader_only: true
    06_doctrine_migrations_migrate_execute:
        command: "USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user) && sudo -u ${USER} -H ebscripts/doctrine_migrations_migrate.sh"
        leader_only: true
files:
    "/opt/elasticbeanstalk/hooks/appdeploy/enact/51_ls_clear_cache.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/usr/bin/env bash
            . /opt/elasticbeanstalk/support/envvars
            set -xe
            EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config  container -k app_deploy_dir)
            EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)

            echo "Application init start..."
            cd $EB_APP_DEPLOY_DIR

            # Remove any cache
            rm -rf app/cache/*
            rm -rf var/cache/*

            test -x ebscripts/clear_cache.sh && sudo -u ${EB_APP_USER} ebscripts/clear_cache.sh
            rm -rf ebscripts

            echo "Application init done."

    "/opt/elasticbeanstalk/hooks/appdeploy/enact/90_ls_update_permissions.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/usr/bin/env bash
            set -xe
            EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config  container -k app_user)
            EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config  container -k app_deploy_dir)
            EB_APP_LOGS=$(/opt/elasticbeanstalk/bin/get-config container -k app_logs_dir)

            cd $EB_APP_DEPLOY_DIR

            # Update permissions! User and group are the same "webapp"
            echo "Change owner and group to ensure, cache and log is writable."
            chown -R $EB_APP_USER:$EB_APP_USER .

            if [ "$EB_APP_LOGS" != "" ]; then
                echo "Change owner and group of logfiles."
                if [ -d $EB_APP_LOGS ]; then
                    chown -R $EB_APP_USER:$EB_APP_USER $EB_APP_LOGS
                else
                    echo "Dir $EB_APP_LOGS does not exist!"
                fi
            fi
