#!/usr/bin/env bash
name=''
http_port='80'
https_port='443'
root_path="$(pwd)"
verbose='false'

while getopts 'n:p:s:r:a:vh' flag; do
  case "${flag}" in
    n) name="${OPTARG}" ;;
    p) http_port="${OPTARG}" ;;
    s) https_port="${OPTARG}" ;;
    r) root_path="${OPTARG}" ;;
    a) phpver="${OPTARG}" ;;
    v) verbose='true' ;;
    h) echo "Variables"
       echo "-n = Name of Container, Required"
       echo "-p = Non-https Port Override, default 80"
       echo "-s = Https Port Override, default 443"
       echo "-r = Root Path for files and database, defaults to current working path"
       echo "-a = PHP App Version, Default to 8.3"
       echo "-v = Enable Verbose Mode"
       exit 1 ;;
  esac
done

#Check if Docker is installed
echo "Checking if Docker is Installed..."
check_docker=$(command -v docker)
if [ $? != 0 ]; then
  echo "Docker must be installed to run this application"
  exit 1
fi
echo "Docker looks to be installed"
if [ -z "$name" ]; then
    echo "Name not set, please set it with -n"
    exit 1
fi

if [ -z "$phpver" ]; then
    phpver=83;
fi

echo "Building Docker Image..."
user=$(whoami)
uid=$(id -u)
if [ ! -d "$root_path/user" ]; then
  mkdir -p "$root_path/user";
  mkdir -p "$root_path/user/logs/{apache,system}";
fi
$check_docker volume create "$name-mysql"
$check_docker run --pull=always -d -p "$http_port":80 -p "$https_port":443 -e PHPVER=$phpver -e environment=DEV --mount type=bind,source="$root_path"/user,target=/home/"$user" --mount type=bind,source="$(pwd)"/user/logs/apache,target=/etc/httpd/logs --mount type=bind,source="$(pwd)"/user/logs/system,target=/var/log  -v"$name-mysql":/var/lib/mysql -e uid="$uid" -e user="$user" -e domain="$name-local.dev" --name "$name" repo.anhonesthost.net/cloud-hosting-platform/cac:latest
echo "Creating management scripts in root directory..."
echo "#!/usr/bin/env bash" > "$root_path/instance_start"
echo "docker start $name" >> "$root_path/instance_start"
echo "#!/usr/bin/env bash" > "$root_path/instance_stop"
echo "docker stop $name" >> "$root_path/instance_stop"
echo "#!/usr/bin/env bash" > "$root_path/instance_logs"
echo "docker exec $name bash -c 'tail -f /etc/httpd/logs/*'" >> "$root_path/instance_logs"
echo "#!/usr/bin/env bash" > "$root_path/instance_db_info"
echo "docker exec $name cat /var/lib/mysql/creds" >> "$root_path/instance_db_info"
chmod +x $root_path/instance_*
echo "Waiting 160 seconds for setup to finish"
sleep 160;
echo "Installing WordPress..."
wpdbuser=$(docker exec $name cat /var/lib/mysql/creds |grep User| awk -F ": " {'print $2'})
wpdbpass=$(docker exec $name cat /var/lib/mysql/creds |grep Password| awk -F ": " {'print $2'})
wpdb=$(docker exec $name cat /var/lib/mysql/creds |grep Database| awk -F ": " {'print $2'})
docker exec $name bash -c "cd /home/$(whoami)/public_html; wp core download; wp config create --dbname=$wpdb --dbuser=$wpdbuser --dbpass=$wpdbpass ; chown -R $(whoami):$(whoami) /home/$(whoami)/public_html;"
echo "Local Development Instance Created, to stop run ./instance_stop from within the base directory"
echo "MySQL DB Credentials"
docker exec $name cat /var/lib/mysql/creds
exit 0