#!/bin/bash 

# INSTALLATION SCRIPT FOR autogre.sh
# Will install the script in /usr/local/bin/autogre.sh
# Will install the systemd service in /etc/systemd/system/autogre.service
# Will install the systemd timer in /etc/systemd/system/autogre.timer

# FUNCTIONS

function copy_auto_gre() {
    curl https://git.gnous.eu/mael/autogre/raw/branch/main/autogre.sh > /usr/local/bin/autogre.sh
    chmod +x /usr/local/bin/autogre.sh
}

function generate_systemd_service() {
    cat << EOF > /etc/systemd/system/autogre.service
[Unit]
Description=Auto GRE tunnel service
After=network.target

[Service]
EnvironmentFile=/etc/default/autogre
Type=simple
ExecStart=/usr/local/bin/autogre.sh

[Install]
WantedBy=multi-user.target
EOF
}

function generate_systemd_timer() {
    cat << EOF > /etc/systemd/system/autogre.timer
[Unit]
Description=Auto GRE tunnel timer

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min

[Install]
WantedBy=timers.target
EOF
}

function enable_systemd_timer_and_service() {
    systemctl enable autogre.service
    systemctl start autogre.service
    systemctl enable autogre.timer
    systemctl start autogre.timer
}

function main() {
    copy_auto_gre
    generate_systemd_service
    generate_systemd_timer
    enable_systemd_timer_and_service
}

main