#!/bin/bash
# a6 - Apache helper

set -e

source "/home/zwelch/src/mcf/mcsh/install/share/mcsh/mcsh.sh"


######
# Configuration

a6_config_init() {
	script_setting_vars www_confdir main_conf tmpl tmpl_conf
}

a6_config_check() {
	test "$PWD" = "$www_confdir" || warn "$PWD != $www_confdir"
	[ -f "$main_conf" ] || warn "$main_conf: does not exist"
	[ -f "$tmpl_conf" ] || warn "$tmpl_conf: does not exist"
}


######
# Native dependencies

a6_server_packages=( apache2 )


######
# CLI

a6_site() { cmd_dispatch "$@"; }
a6_site_new() {
	local site
	site=$1
	[ "$site" ] || error "usage: $script <vhost>"

	local site_conf
	site_conf="$www_confdir/$site.conf"
	[ ! -f "$site_conf" ] || error "$site_conf: exists"

	local vhost
	vhost="$site.$domain"
	host "$vhost" || error "$vhost: unable to resolve name"

	[ -f "$tmpl_conf" ] || error "$tmpl_conf: does not exist"

	# change everything but the SSL cert
	sed \
		-e "s,$tmpl,$site,g" \
		-e "s,live/$site,live/$tmpl," \
		$tmpl_conf >$site_conf

	# include from main configuration file
	if ! grep "Include $site_conf" $main_conf; then
		echo "Include $site_conf" >>$main_conf
	fi
	# create logs
	mkdir -p "../logs/$site"

	sudo apachectl configtest
	sudo service apache2 reload

	cert="/etc/letsencrypt/live/$vhost/fullchain.pem"
	if [ ! -d "/etc/letsencrypt/live/$vhost" ]; then
		sudo letsencrypt -d "$vhost" || true
		[ -f "$cert" ] || \
			error "ERROR: letsencrypt failed to obtain a certificate"

	else
		echo "$cert: exists"
	fi
	sed -i -e "s,live/$tmpl,live/$site," $site_conf

	# reload new certificates
	sudo service apache2 reload
}

######
# Main

a6_desc() { echo "Apache site manager"; }

a6_usage() {
	cat <<USAGE
<cmd> ...
Commands:
	site
USAGE
}

a6_help() {
	cat <<HELP

The $script_name tool manages sets of apache configuration files, allowing the
easy creation and deployment of new sites based on existing templates.
HELP
}

app_run "$@"

View the Script Reference Index


Generated on Tue Apr 25 21:19:41 PDT 2017 by mcsh i7 v0.18.0.