#!/bin/sh
echo This script configures your local repository and uses gitlab and dokku to initialize and upload your local git repository configured to automatically deploys to your remote server to be built and run in a docker container securely serving your app to your domain at a public IP.
echo you need a server running Ubuntu 20.04 with your machine\'s ssh key added to the server
echo you need a DNS A record pointing your domain to your server\'s ip address
echo you need to have your machine\'s ssh keys registered with gitlab and a gitlab personal access token
echo
echo WHAT EZINNIT DOES:
echo checks for ezinnit.config, if it doesn\'t exist, it prompts you for the values and creates an ezinnit.config file
echo uses toptotal to create a .gitignore file in your project directory
echo creates a procfile for your app in project directory
echo runs optional app creation templates for your app
echo creates a .gitlab-ci.yml \(gitlab pipeline\) file in your project directory
echo creates a requirements.txt file in your project directory
echo creates a new gitlab repository and commits your project to main
echo gets the runner token for the new repository from gitlab
echo creates ssh keys on remote server
echo downloads and installs dokku on remote server
echo creates dokku app on remote server
echo sets the domain for the dokku app on remote server
echo sets the apps proxy port to 80:5000 on remote server
echo downloads and creates a gitlab runner on remote server
echo registers the gitlab runner on remote server
echo downloads and installs dokku-letsencrypt
echo enables encryption for app with TLS certificate from letsencrypt
echo adds a chron job to automatically renew TLS certificates
echo
echo
echo created by john hewitt and robert carroll for synctivate
echo synctivate custom software
echo https://synctivate.com
echo
echo

username=""
gitlab_domain=""
token=""
appname=""
ip=""
domain=""
email=""
app_type="ezinnit"
oneclick=""


if [ -f ezinnit/ezinnitglobal.config ]
then
  echo "ezinnitglobal.config exists"
  . ezinnit/ezinnitglobal.config
fi
if [ -f ezinnit/ezinnit.config ]
then
  echo config file found...
  # shellcheck disable=SC2039
  . ezinnit/ezinnit.config

else
  echo no config file found, prompting for variables

  echo enter gitlab username
  read -r username

  echo enter gitlab domain \(\"gitlab.com\" or \"git.mydomain.com\"\)
  read -r gitlab_domain

  echo enter gitlab personal access token
  read -r token

  echo new server\'s ip address
  read -r ip

  echo create DNS \"A\" record now before continuing for best results
  echo enter domain\(or subdomain\) for new app. example: \"mysubdomain.mydomain.com\"
  read -r domain

  echo enter email for letsencrypt.org
  read -r email

  if [ "$oneclick" = "django" ]
  then
    echo django.innit detected...
  else
    echo app name becomes the name of your gitlab repository and the dokku app name on the server
    echo App name must begin with lowercase alphanumeric character, and cannot include uppercase characters, colons, or underscores
    echo enter app name
    read -r appname

    echo WARNING: if you select an app, ezinnit will write over your procfile, settings.py, main.py etc.
    echo enter app type \(flask, django, fastApi, or just hit enter to deploy your environment as is\)
    read -r app_type
  fi
  echo
  echo
  echo Is everything correct?
  echo
  echo gitlab username: "$username"
  echo gitlab domain: "$gitlab_domain"
  echo gitlab personal token: "$token"
  echo app name: "$appname"
  echo server ip: "$ip"
  echo domain: "$domain"
  echo email: "$email"
  if [ "$app_type" = "ezinnit" ]
  then
    echo app type: deploying environment as is
  else
    echo app type: "$app_type"
  fi
  echo
  read -r -p "Check the above settings and then press enter to deploy..."
fi
echo "username=$username
gitlab_domain=$gitlab_domain
token=$token
appname=$appname
ip=$ip
domain=$domain
email=$email
app_type=$app_type" >> ezinnit/ezinnit.config

# check for gitignore
if [ -f .gitignore ]
then
  echo .gitignore found...
else
  echo no .gitignore found, creating...
  echo creating gitignore from www.toptotal.com/developers/gitignore
  echo "function gi() { curl -sL https://www.toptal.com/developers/gitignore/api/\$@ ;}" >> \
  ~/.bashrc && . ~/.bashrc
  echo creating gitignore
  gi python,pycharm >> .gitignore
fi

if [ "$app_type" = "flask" ]
then
  echo getting ezinnit flask template script
  mkdir ezinnit/platform_templates
  wait
  mkdir ezinnit/platform_templates/flask
  wait
  wget -P ezinnit/platform_templates/flask https://raw.githubusercontent.com/johnsyncs/ezinnit/main/ezinnit%20template%20scripts/flask.ezinnit
  echo running ezinnit flask template script
  bash ezinnit/platform_templates/flask/flask.ezinnit
elif [ "$app_type" = "fastApi" ] || [ "$app_type" = "fastAPI" ] || [ "$app_type" = "fastapi" ]
then
  echo getting ezinnit fastapi template script
  mkdir ezinnit/platform_templates
  mkdir ezinnit/platform_templates/fastapi
  wget -P ezinnit/platform_templates/fastapi https://raw.githubusercontent.com/johnsyncs/ezinnit/main/ezinnit%20template%20scripts/fastapi.ezinnit
  echo running ezinnit fastapi template script
  bash ezinnit/platform_templates/fastapi/fastapi.ezinnit
elif [ "$app_type" = "django" ]
then
  echo getting django template from ezinnit
  mkdir ezinnit/platform_templates
  mkdir ezinnit/platform_templates/django
  wget -P ezinnit/platform_templates/django https://raw.githubusercontent.com/johnsyncs/ezinnit/main/ezinnit%20template%20scripts/django.ezinnit
  echo running django template script
  bash ezinnit/platform_templates/django/django.ezinnit
elif [ "$app_type" = "ezinnit" ]
then
  echo deploying...
fi

echo creating gitlab deployment pipeline
echo "image: dokku/ci-docker-image

stages:
  - deploy

variables:
  GIT_DEPTH: 0

deploy:
  stage: deploy
  only:
    - main
  variables:
    GIT_REMOTE_URL: ssh://dokku@$ip:22/$appname
  script: dokku-deploy
  after_script: [dokku-unlock]" > .gitlab-ci.yml

if [ -f .requirement.txt ]
then
  echo requirements.txt found...
else
  echo creating requirements.txt
  pip freeze > requirements.txt
fi

echo creating new gitlab repository
# initialize git appname
# shellcheck disable=SC2164
git init --initial-branch=main
git push --set-upstream git@$gitlab_domain:$username/$appname.git
git remote add origin git@$gitlab_domain:$username/$appname.git
git add .
git commit -m "Initial commit"
git push -u origin main


echo getting gitlab runner token
#https://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools
runner_token=$(curl --header "PRIVATE-TOKEN: $token" "https://$gitlab_domain/api/v4/projects/$username%2F$appname" | \
python3 -c "import sys, json; print(json.load(sys.stdin)['runners_token'])")

echo getting ezinnit script for server
mkdir ezinnit/deployment
wget -P ezinnit/deployment https://raw.githubusercontent.com/johnsyncs/ezinnit/main/ezinnit%20deployment%20scripts/initialize-server.sh

echo creating create_config file for server
echo "
mkdir ezinnit
echo \"
username=$username
gitlab_domain=$gitlab_domain
token=$token
appname=$appname
ip=$ip
domain=$domain
email=$email
app_type=$app_type
runner_token=$runner_token\" >> ezinnit/ezinnit.config" >> ezinnit/deployment/create_config.sh

echo running create_config on server
ssh root@"$ip" "bash -s" < ezinnit/deployment/create_config.sh
echo running initialize-server.sh on server
ssh root@"$ip" "bash -s" < ezinnit/deployment/initialize-server.sh
wait
git add .
git commit -m "commit ezinnit"
git push -u origin main
echo
echo
echo
echo your gitlab repository:
echo https://"$gitlab_domain"/$username/$appname
echo
echo your domain:
        echo https://"$domain"
echo
echo your server ip:
echo http://"$ip"
echo
echo that\'s ezinnit!
echo https://synctivate.com

if [ -f ezrun ]
then
  bash ezrun
fi