#!/usr/bin/env bash

## Install the cert addon globally, create a cert for this project, then restart the project to enable the cert for this local site.
##
## Usage: fin cert

# Console colors
red=`tput setaf 1`
green=`tput setaf 2`
green_bg=`tput setaf 1; tput setab 2`
yellow=`tput setaf 3; tput setab 234`
NC=`tput sgr0`

ADDON_DIR=$HOME"/.docksal/addons/mkcert"

if [ ! -d ${ADDON_DIR} ]; then 
	# Instal the mkcert globally
	echo -e  "${green_bg} Adding mkcert addon! ${NC}${yellow} ${NC}"
	fin addon install mkcert -g -y
fi

# generate cert for a project
echo -e  "${green_bg} Create SSL certificate ${NC}${yellow} ${NC}"
fin mkcert create

# reset vhost-proxy to pick-up the new cert
echo -e  "${green_bg} Reset vhost-proxy ${NC}${yellow} ${NC}"
fin system reset vhost-proxy

# see if this works?
echo -e  "${green_bg} Restart project ${NC}${yellow} to enable new cert ${NC}"
fin restart