---
# System prepended metadata

title: Untitled

---

#!/bin/bash

APP_URL="$1"
GIT_URL="$2"
TARGET_BRANCH="$3"

if [ -z "$APP_URL" ]; then
  echo "APP URL is not set"
  exit 2
fi

if [ -z "$GIT_URL" ]; then
  echo "GIT_URL is not set"
  exit 2
fi

if [ -z "$TARGET_BRANCH" ]; then
  echo "TARGET_BRANCH is not set"
  exit 2
fi



DOMAIN=$(sed "s/https:\/\///g"  <<<"$APP_URL")
DOMAIN=$(sed "s/http:\/\///g"  <<<"$DOMAIN")
DOMAIN=$(sed "s/\///g" <<<"$DOMAIN")

DOMAIN_PARTS=$(echo "$DOMAIN" | tr "." "\n")

if [ ${DOMAIN_PARTS[2]} = "acid21" ]; then
  CURRENT_USER="${DOMAIN_PARTS[0]}-${DOMAIN_PARTS[1]}"
else
  CURRENT_USER="$4"
fi

if [ -z "$CURRENT_USER" ]; then
  echo "non standard url and default user not set"
  exit 2
fi

DB=$(sed s/-/_/g  <<<"$CURRENT_USER")

if ssh "$CURRENT_USER@DOMAIN" "grep -q $DB ~/.my.cnf"; then
  echo "database config looks ok"
else
  echo "database not found to be configured in .my.cnf"
  exit 1
fi

#prep shared dir
if ssh "$CURRENT_USER@DOMAIN" "test -d"

#mkdir -p ~/apps/CURRENT_USER/releases

if [ ! -d ~/sw6-prod/ ]; then
  git clone "$GIT_URL" -b "$TARGET_BRANCH" ~/sw6-prod
else
  cd ~/sw6-prod/ && git pull origin "$TARGET_BRANCH"
fi

cd ~/sw6-prod
#check ssh connection
SSH_CHECK=$(ssh -q CURRENT_USER@DOMAIN exit)

if [ "$SSH_CHECK" == 255 ]; then
  cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
fi

if [ ! -f ~/sw6-prod/deploy.php ]; then
  cp ~/deployment-setup/deploy.php ~/sw6-prod/
  cp ~/deployment-setup/deploy-shopware6.php ~/sw6-prod/

  sed -i "s,GIT_URL,$GIT_URL,g" ~/sw6-prod/deploy.php
  sed -i "s,APP_NAME,$APP_NAME,g" ~/sw6-prod/deploy.php
fi

sed -i "s/__SSHHOST__/$DOMAIN/g" ~/sw6-prod/deploy.php
sed -i "s/__SSHUSER__/$CURRENT_USER/g" ~/sw6-prod/deploy.php

if [ ! -f ~/sw6-prod/composer.phar ]; then
  php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  php composer-setup.php
  php -r "unlink('composer-setup.php');"
  mv composer.phar ~/
fi
env APP_URL="$APP_URL"

### remove the current link to public, deployer will do that for us
rm -rf ~/apps/"$CURRENT_USER"/public

cd ~/sw6-prod/ && php ~/composer.phar install
cd ~/sw6-prod/ && php ~/composer.phar require deployer/deployer
cd ~/sw6-prod/ && php ./vendor/deployer/deployer/bin/dep deploy -vvv

### echo what to do next
echo "export APP_URL=$APP_URL && cd ~/apps/$CURRENT_USER/release/ && rm .env && composer install && bin/console system:setup &&  mv  ~/apps/$CURRENT_USER/releases/1/.env  ~/apps/$CURRENT_USER/shared/"
### installation can be done 
echo "php recovery/install/index.php --db-host=localhost --db-user=c374nr8c3n893 --db-password=2cr8374br9c83b9 --db-name=mitte_dev --no-skip-import --shop-locale=de-DE --shop-name="Mitte Mitte" --shop-currency=EUR --shop-country=DEU --admin-username=admin --admin-password=changeme123 --admin-email=michael.mueller@acid21.com --admin-locale=de-DE --admin-firstname=Admin --admin-lastname=User --no-interaction"

#cp -r ~/apps/"$CURRENT_USER"/releases/1 ~/apps/"$CURRENT_USER"/releases/initial

#chmod 660 shared/config/jwt/*