138 lines
3.3 KiB
Bash
Executable File
138 lines
3.3 KiB
Bash
Executable File
#!/bin/bash
|
|
# shellcheck disable=SC1090
|
|
|
|
## Definitions
|
|
|
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
|
PLIST=${DIR}/plugins/plugin.list
|
|
ELIST=${DIR}/extensions/extension.list
|
|
DEV=true
|
|
|
|
## Colors
|
|
|
|
declare -A COLOR
|
|
COLOR=(
|
|
[nc]='\033[0m'
|
|
[erro]='\033[31m'
|
|
[warn]='\033[33m'
|
|
[done]='\033[32m'
|
|
[info]='\033[34m'
|
|
)
|
|
|
|
## Output types
|
|
|
|
if ${DEV}; then
|
|
function output () {
|
|
while read LINE; do
|
|
if [[ ${LINE%% *} =~ ^erro|done|info|warn$ ]]; then
|
|
echo -e "${COLOR[${LINE%% *}]}$(date +%F\ %T\ %z) | ${LINE%% *} | ${LINE#* }${COLOR[nc]}"
|
|
else
|
|
echo -e "${COLOR[nc]}$(date +%F\ %T\ %z) | ${LINE}${COLOR[nc]}"
|
|
fi
|
|
done
|
|
}
|
|
function xt () {
|
|
echo "$1 Set return code to $2"
|
|
return "$2"
|
|
}
|
|
else
|
|
function output () {
|
|
while read LINE; do
|
|
echo -e "${COLOR[${LINE%% *}]}${LINE#* }${COLOR[nc]}"
|
|
done
|
|
}
|
|
function xt () {
|
|
return "$2"
|
|
}
|
|
fi
|
|
|
|
# Main run
|
|
|
|
function main () {
|
|
export CWDIR=${DIR}/plugins/${1}
|
|
if (containsElement "$1" "${plugins[@]}"); then
|
|
if [ "$2" = "help" ]; then
|
|
echo "info $1"
|
|
else
|
|
[ -f "${CWDIR}/.env" ] && source "${CWDIR}/.env"
|
|
source "${CWDIR}/main.sh"
|
|
fi
|
|
else
|
|
echo "erro Plugin $1 not found."
|
|
avail-plugins
|
|
echo "info Tip: If you added the plugin recently, add it to the index with '${0##*/} index'"
|
|
fi
|
|
}
|
|
|
|
function containsElement () {
|
|
local e match="$1"
|
|
shift
|
|
for e; do [[ "$e" == "$match" ]] && return 0; done
|
|
return 1
|
|
}
|
|
|
|
function nopluginlist {
|
|
echo "warn No plugin list found. Try running '${0##*/} index' to index everything."
|
|
}
|
|
|
|
function avail-plugins {
|
|
if [ -f "${PLIST}" ]; then
|
|
echo "warn Available plugins:"
|
|
mapfile -t plugins < "${PLIST}" || echo "erro Failed to load plugin list."
|
|
echo "warn ${plugins[@]}"
|
|
else
|
|
nopluginlist
|
|
fi
|
|
}
|
|
|
|
function index {
|
|
echo "info Indexing plugins"
|
|
truncate -s 0 "${PLIST}"
|
|
for i in $(find "${DIR}"/plugins/* -type d | grep -P "(?<=\/plugins\/)([\w\d\-\_])*" -o); do
|
|
echo "$i" >> "${PLIST}" && echo "done Added plugin '$i'."
|
|
done
|
|
echo "info Indexing extensions"
|
|
truncate -s 0 "${ELIST}"
|
|
for i in $(find "${DIR}"/extensions/* -type f | grep -P "(?<=\/extensions\/)([\w\d\-\_])*(\.func)" -o); do
|
|
echo "$i" >> "${ELIST}" && echo "done Added extension '$i'."
|
|
done
|
|
}
|
|
|
|
function loadlists {
|
|
mapfile -t plugins < "${PLIST}" || echo "erro Failed to load plugin list."
|
|
mapfile -t extensions < "${ELIST}" || echo "erro Failed to load extension list."
|
|
for i in "${extensions[@]}"; do
|
|
source "${DIR}/extensions/$i" || echo "warn Failed to import extension ${i}."
|
|
done
|
|
}
|
|
|
|
function preparedirs {
|
|
if mkdir "${DIR}"/plugins/ "${DIR}"/extensions/; then
|
|
echo "info Created a empty plugins and extensions directory."
|
|
else
|
|
echo "erro Could not create directory."
|
|
fi
|
|
}
|
|
|
|
## Main run
|
|
|
|
(
|
|
[ ${DEV} = "true" ] && echo "warn Running in development mode!"
|
|
if ! [ $# -eq 0 ] && ! [[ "$1" = "index" ]] && [ -f "${PLIST}" ] && [ -f "${ELIST}" ]; then
|
|
loadlists
|
|
main "$@"
|
|
elif [ -d "${DIR}"/plugins/ ] && [ -d "${DIR}"/extensions/ ]; then
|
|
if [ $# -eq 0 ]; then
|
|
echo "warn Nothing to do!"
|
|
avail-plugins
|
|
elif [[ "$1" = "index" ]]; then
|
|
index
|
|
else
|
|
nopluginlist
|
|
fi
|
|
else
|
|
preparedirs
|
|
fi
|
|
! [[ "$1" = "rawpl" ]] && echo "info Made with ♥ by casKd | t.me/casKd_dev"
|
|
) | output
|