#!/bin/bash


sudo_file=/etc/sudoers


# Add Command Aliases
add_cmd_alias() {
    {% for sudo in sudo_alias %}
    if $(grep '^Cmnd_Alias {{ sudo.name }}' ${sudo_file} &> /dev/null); then
        sed -i 's@^Cmnd_Alias.*{{ sudo.name }}.*@Cmnd_Alias {{ sudo.name }} = {{ sudo.commands }}@g' ${sudo_file}
    else
        echo "Cmnd_Alias {{ sudo.name }} = {{ sudo.commands }}" >> ${sudo_file}
    fi
    {% endfor %}
}


add_role_chosen() {
    {% for role, alias in role_chosen_aliase.items %}
        if $(grep '^{{ role }}.*' ${sudo_file} &> /dev/null); then
            sed -i 's@^{{ role }}.*@{{ role }} ALL =  NOPASSWD: {{ alias }}@g' ${sudo_file}
        else
            echo "{{ role }} ALL =  NOPASSWD: {{ alias }}"  >> ${sudo_file}
        fi
    {% endfor %}
}


add_cmd_alias
add_role_chosen