#!/bin/ash
#Setup Nginx repo
echo "@nginx http://nginx.org/packages/mainline/alpine/v$(egrep -o '^[0-9]+\.[0-9]+' /etc/alpine-release)/main" >> /etc/apk/repositories
mv /opt/Setup/nginx_signing.rsa.pub /etc/apk/keys/nginx_signing.rsa.pub

#Install Nginx
apk add nginx@nginx

#Configure Nginx
mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled /etc/nginx/snippets /etc/nginx/modules-available /etc/nginx/modules-enabled /etc/nginx/snippets/
rm -rf /etc/nginx/conf.d/*
mv /opt/Setup/Configs/nginx.conf /etc/nginx/nginx.conf
mv /opt/Setup/Configs/nginx_template_* /etc/nginx/sites-available/

#Install AcmeSH
mkdir -p /opt/acmesh /etc/acmesh/data /etc/acmesh/certs
tar -zxf /opt/Setup/acmesh.tar.gz -C /opt/acmesh --strip-components=1
rm -rf /opt/Setup/acmesh.tar.gz

#Configure AcmeSH
mv /opt/Setup/Configs/profile ~/.profile
mv /opt/Setup/Configs/acmesh.conf /etc/acmesh/data/account.conf
/opt/acmesh/acme.sh --home "/opt/acmesh/" --config-home "/etc/acmesh/data" --upgrade
/opt/acmesh/acme.sh --config-home "/etc/acmesh/data" --set-default-ca  --server  letsencrypt
echo '42      0       *       *       *      "/opt/acmesh/acme.sh" --cron --home "/opt/acmesh/" --config-home "/etc/acmesh/data" > /dev/null' >> /etc/crontabs/root

#Add `AddDomain` script
mv /opt/Setup/Scripts/AddDomain.sh /opt/AddDomain.sh

#Enable nginx on boot
rc-update add nginx