#!/bin/sh

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2016-present Team LibreELEC (https://libreelec.tv)

. /etc/profile
oe_setup_addon service.system.tinc

DIR_LEGACY="$ADDON_HOME/.config"
if [ -d "$DIR_LEGACY" ]; then
  echo "Warning: using legacy configuration $DIR_LEGACY, ignoring Kodi settings"
  tincd --config "$DIR_LEGACY" --no-detach
  exit $?
fi

[ -z "$tinc_loglevel" ] && tinc_loglevel="3"

NET="vmnet_le"
NAME="${tinc_subnet//./_}"
CONF_KEYS="/storage/.cache/tinc.$NET"
CONF_TINC="/run/tinc/$NET"
DATA_LOCAL="$ADDON_HOME/Local"
DATA_REMOTES="$ADDON_HOME/Remotes"

rm -fr "$CONF_TINC" "$DATA_LOCAL" &> /dev/null

if [ ! -d "$CONF_KEYS" ]; then
  rm -fr "$CONF_KEYS" &> /dev/null
  mkdir -p "$CONF_KEYS"
  tinc --batch --config "$CONF_KEYS" generate-ed25519-keys
fi

if [ ! -d "$DATA_REMOTES" ]; then
  rm -fr "$DATA_REMOTES" &> /dev/null
  mkdir -p "$DATA_REMOTES"
fi

if [ "$tinc_subnet" == "0.0.0.0" ]; then
  echo "Warning: default subnet $tinc_subnet, aborting"
  exit
fi

if [ -e "$DATA_REMOTES/$NAME" ]; then
  echo "Error: $NAME in $DATA_REMOTES, aborting"
  exit
fi

mkdir -p "$CONF_TINC"
cp -RT "$ADDON_DIR/config" "$CONF_TINC"
chmod +x "$CONF_TINC"/*-*
ln -sf "$DATA_REMOTES" "$CONF_TINC/hosts"
tee >> "$CONF_TINC/tinc.conf" << EOF
Ed25519PrivateKeyFile = $CONF_KEYS/ed25519_key.priv
LogLevel              = $tinc_loglevel
Name                  = $NAME
EOF

mkdir -p "$DATA_LOCAL"
if [ -n "$tinc_address" ]; then
  echo "Address          = $tinc_address" >> "$DATA_LOCAL/$NAME"
fi
cat "$CONF_KEYS/ed25519_key.pub" >> "$DATA_LOCAL/$NAME"

tee -a "$CONF_TINC/tinc.conf" >> "$DATA_LOCAL/$NAME" << EOF
Port             = $tinc_port
Subnet           = $tinc_subnet
EOF

tincd --net "$NET" --no-detach
