#!/bin/bash

if [[ "$(hostname -s)" =~ ^[a-z0-9\-]*$ ]] && [[ "$(hostname -d)" =~ ^[0-9a-z\.\-]*[\.][0-9a-z\-]*[0-9a-z]$ ]]; then
    echo "Имя компьютера $(hostname -f) содержит доменный суффикс, продолжаем установку"
 else
    echo "Имя компьютера $(hostname -f) не содержит доменный суффикс, установка невозможна, для повторной установки запустите dd-manifests-install"
    exit 1
fi

if [ -d /etc/puppetlabs/puppet ]; then
    puppet_conf='/etc/puppetlabs/puppet/puppet.conf'
    autosign_conf='/etc/puppetlabs/puppet/autosign.conf'
    code_path='/etc/puppetlabs/code'
else
    puppet_conf='/etc/puppet/puppet.conf'
    autosign_conf='/etc/puppet/autosign.conf'
    code_path='/etc/puppet/code'
fi

# autosign
cat ${autosign_conf} 2>/dev/null | grep "^*.$(hostname -d)"
if [ $? != 0 ]; then
    echo "Set autosign = *.$(hostname -d)"
    echo "*.$(hostname -d)" >> "${autosign_conf}"
fi

# [main]
LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} main server 2>/dev/null
if [ $? != 0 ]; then
    echo "Set main/server = $(hostname -f)"
    crudini --ini-options=ignoreindent --set ${puppet_conf} main server $(hostname -f)
fi

LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} main certname 2>/dev/null
if [ $? != 0 ]; then
    echo "Set main/certname = $(hostname -f)"
    crudini --ini-options=ignoreindent --set ${puppet_conf} main certname $(hostname -f)
fi

LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} main codedir 2>/dev/null
if [ $? != 0 ]; then
    echo "Set main/codedir = ${code_path}"
    crudini --ini-options=ignoreindent --set ${puppet_conf} main codedir ${code_path}
fi

LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} main environmentpath 2>/dev/null
if [ $? != 0 ]; then
    echo "Set main/environmentpath = ${config_version}/environments"
    crudini --ini-options=ignoreindent --set ${puppet_conf} main environmentpath ${code_path}/environments
fi

# [agent]
LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} agent environment 2>/dev/null
if [ $? != 0 ]; then
    echo "Set agent/environment = production"
    crudini --ini-options=ignoreindent --set ${puppet_conf} agent environment production
fi

LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} agent masterport 2>/dev/null
if [ $? != 0 ]; then
    echo "Set agent/masterport = 8140"
    crudini --ini-options=ignoreindent --set ${puppet_conf} agent masterport 8140
fi

# [server]
LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} server autosign 2>/dev/null
if [ $? != 0 ]; then
    echo "Set server/autosign = ${autosign_conf} { mode = 0664 }"
    crudini --ini-options=ignoreindent --set ${puppet_conf} server autosign "${autosign_conf} { mode = 0664 }"
fi

LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} server ca 2>/dev/null
if [ $? != 0 ]; then
    echo "Set server/ca = true"
    crudini --ini-options=ignoreindent --set ${puppet_conf} server ca true
fi

LANG=C crudini --ini-options=ignoreindent --get ${puppet_conf} server certname 2>/dev/null
if [ $? != 0 ]; then
    echo "Set server/certname = $(hostname -f)"
    crudini --ini-options=ignoreindent --set ${puppet_conf} server certname $(hostname -f)
fi

systemctl enable puppet.service puppetserver.service

if systemctl is-active --quiet puppetserver; then
    echo "Restart puppet server"
    systemctl restart puppetserver.service
else
    echo "Start puppet server"
    systemctl start puppetserver.service
fi

if systemctl is-active --quiet puppet; then
    echo "Restart puppet"
    systemctl restart puppet.service
else
    echo "Start puppet"
    systemctl start puppet.service
fi

exit 0
