#!/usr/bin/env bash
apt update
apt install -y wireguard-tools
# apt install -y wireguard-dkms


LISTENPORT=51820
PEERPORT=52037
SAVECONFIG=true
ADDRESS="10.0.0.5/24"
PEER="tagmar.net"
ALLOWED="10.0.0.1/32"
PRIVATEKEY=$(wg genkey)
PUBLICKEY=$(echo ${PRIVATEKEY} | wg pubkey)
PEERPUBLIC="IUV3V1SwE1b+6HbJPoVg5XGrkSS8re3oV4JamDbmrC0="

#(umask 077 && printf "[Interface]\nPrivateKey = " | sudo tee /etc/wireguard/wg0.conf > /dev/null)
#wg genkey | sudo tee -a /etc/wireguard/wg0.conf | wg pubkey | sudo tee /etc/wireguard/publickey

cat > /etc/wireguard/wg0.conf<<"EOF"
[Interface]
PrivateKey = ${PRIVATEKEY}
ListenPort = ${LISTENPORT}
SaveConfig = ${SAVECONFIG}

Address = ${ADDRESS}


[Peer]
PublicKey = ${PEERPUBLIC}
AllowedIPs = ${ALLOWED}
Endpoint = ${PEER}:${PEERPORT}
EOF

echo ${PUBLICKEY} > /etc/wireguard/publickey


