#!/bin/bash
set -e

# usually /usr/share/SOPS, but can be anywhere
installpath=`dirname $0`/..
installpath=`readlink -f $installpath` # get absolute path
inst_pluginpath=$installpath/plugins/plugins-available
# user-local path to available plugins
plugins_available="${XDG_CONFIG_HOME:-$HOME/.config}/SOPS/plugins-available"
plugins_enabled="${XDG_CONFIG_HOME:-$HOME/.config}/SOPS/plugins-enabled"
filename="${1##*/}"
# Error checking, make sure the plugin actually exists
if ! [ -f "$inst_pluginpath/$1" ]; then
    echo "Plugin $filename not found."
    exit 1
fi

if [ -e "$plugins_enabled/$filename" ]; then
    echo "$1 is already enabled."
    exit 1
else
    mkdir -p "$plugins_enabled/"
    ln -s "$inst_pluginpath/$filename" "$plugins_enabled/"
fi
exit 0
