Skip to content

Commit

Permalink
Renamed scripts configng and armbian-interface to confignng+ cli / tui
Browse files Browse the repository at this point in the history
  • Loading branch information
Tearran committed Apr 12, 2024
1 parent 74fa1f4 commit 93e87c6
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 272 deletions.
244 changes: 50 additions & 194 deletions bin/armbian-configng
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,222 +1,78 @@
#!/bin/bash

# This script provides a command-line interface for managing Armbian configuration.
# It loads libraries of functions from the lib directory and displays them in a menu.
# The user can select a function to run, or use a text-based user interface (TUI) to navigate the menus.
# The script also provides a help option and a debug option for developers.
# The script requires sudo privileges to run some functions.
# The script uses whiptail or dialog for the TUI, depending on which is available.

#set -x
#set -e

tput init
#
# Enable Dynamic directory root use home ~/ , /bin , /usr/sbin etc..
bin="$(dirname "${BASH_SOURCE[0]}")"
directory="$(cd "$bin/../" && pwd )"
file_name="$(basename "${BASH_SOURCE[0]}")"
filename="${file_name%.*}"
libpath=$(cd "$directory/lib/$filename/" && pwd)
#sharepath=$(cd "$directory/share/${filename%-dev}/" && pwd)

# Language-based variable assignment for script directory path
# This serves as a Rosetta Stone for developers,
# allowing them to use the variable name they are most comfortable with.

#
# Consept Distribution Compatibility checks
check_distro() {
# allows CTRL c to exit
trap "exit" INT TERM

[[ -f "/usr/bin/${filename%%-*}-config" ]] && distro_config="${filename%%-*}"
[[ -f "/etc/${filename%%-*}-release" ]] && distro_release="${filename%%-*}"
# if both true then we are good to go
[[ -z "$distro_config" ]] || [[ -z "$distro_release" ]] && echo "W: Costum build, Tech support links are missing."
[[ -n "$distro_config" ]] && [[ -n "$distro_release" ]] && echo "I: This build seems to be community supported" | ./armbian-interface -o
[[ -f "/usr/sbin/${filename%%-*}-config" ]] && distro_config="${filename%%-*}"
[[ -f "/etc/${filename%%-*}-release" ]] && distro_release="${filename%%-*}"
# Get the script directory
script_dir="$(dirname "$0")"

}

[[ "$1" == "--dev" ]] && dev=1 && shift 1
# Define the lib directory one level up from the script directory
lib_dir="$script_dir/../lib/armbian-configng"
doc_dir="$script_dir/../share/doc/armbian-configng"
# Check for the existence of the config.ng.jobs.json file in the lib directory
json_file="$lib_dir/config.ng.jobs.json"

#
# Check if the script is dev version.
suffix="${file_name##*-}"
# Load The Bash procedure Objects
json_data=$(cat "$json_file")

if [[ "$suffix" == dev ]]; then
dev=1
check_distro #| armbian-interface -o
fi

if [[ "$(id -u)" != "0" ]] && [[ "$dev" == "1" ]] ; then

cat << EOF #| ./armbian-interface -o
I: Running in UX development mode
W: Admin functions will not work as expected
#
# We're checking if the 'whiptail' command is available on the system.
# 'whiptail' is a simple dialog box utility that works well with Bash. It doesn't have all the features of some other dialog box utilities, but it does everything we need for this script.
# If 'whiptail' is available, we assign its name to the variable DIALOG. This way, we can easily switch to another dialog box utility in the future if we need to, just by changing this one line of code.
[[ -x "$(command -v whiptail)" ]] && DIALOG="whiptail"

EOF
elif [[ "$(id -u)" == "0" ]] && [[ "$dev" == "1" ]] ; then
cat << EOF | ./armbian-interface -o
I: Running in UX development mode
W: Document files may need Admin privleges to edit/remove

EOF
#
# Prepare the module options array
declare -A module_options

fi

#
# Check if the script is being run as root
# UX Development mode bypasses root check, many functions will not work as expected
# Load configng core functions and module options array

if [[ "$(id -u)" != "0" ]] && [[ "$dev" != "1" ]]; then
echo -e "E: This tool requires root privileges. Try: \"sudo $filename\"" >&2
exit 1
fi

declare -A dialogue
source "$lib_dir/config.ng.functions.sh"
set_runtime_variables
echo "Loaded Runtime variables..." | show_infobox ;
set_newt_colors 2
echo "Loaded Dialog..." | show_infobox ;
source "$lib_dir/config.ng.docs.sh"
echo "Loaded Docs..." | show_infobox ;
source "$lib_dir/config.ng.network.sh"
echo "Loaded Network helpers..." | show_infobox ;

#
# Check if whiptail or dialog is installed and set the variable 'dialogue' accordingly.
# todo add a fallback TUI and GUI
if command -v whiptail &> /dev/null; then
dialogue="whiptail"
elif command -v dialog &> /dev/null; then
dialogue="dialog"
else
echo "TUI not found"
echo "Warning: Using fallback TUI"
sleep 1
clear && generate_read
fi

source "$libpath/functions.sh"
source "$libpath/documents.sh"
for file in "$libpath"/*/*.sh; do
source "$file"
done

#
# mapfile -t categories < <(ls -d "$libpath"/* )
mapfile -t categories < <(find "$libpath"/* -type d)
declare -A functions

for category in "${categories[@]}"; do
category_name="${category##*/}"

category_file="$category/readme.md"
if [[ -f "$category_file" ]]; then
category_description=$(grep -oP "(?<=# @description ).*" "$category_file")
fi

for file in "$category"/*.sh; do
description=""
while IFS= read -r line; do
if [[ $line =~ ^#\ @description\ (.*)$ ]]; then
description="${BASH_REMATCH[1]}"
elif [[ $line =~ ^function\ (.*::.*)\(\)\{$ ]]; then
# END: be15d9bcejpp
function_name="${BASH_REMATCH[1]}"
key="$category_name:${file##*/}:${function_name}"
functions["$key,function_name"]=$(echo "$function_name" | sed 's/.*:://')
functions["$key,group_name"]=$(echo "$function_name" | sed 's/::.*//')
functions["$key,description"]=$description
elif [[ $line =~ ^#\ @options\ (.*)$ ]]; then
functions["$key,options"]="${BASH_REMATCH[1]}"
fi
done < "$file"
functions["$key,category"]=$category_name
functions["$key,category_description"]=$category_description
done
done
# Loads the varibles from beta armbian-config for runtime handeling

source "$lib_dir/config.ng.runtime.sh" ;
echo "Loaded Runtime conditions..." | show_infobox ;

#
# WIP: Check arguments for no flag options
# armbian-config --help
# Change to BASH: /usr/sbin/armbian-config main=System selection=BASH
handle_no_flag(){
if [[ "$1" == *"="* ]]; then
IFS='=' read -r key value <<< "$1"
function_name=$(parse_action "$key" "$value")
# Call the function using variable indirection
${function_name}
elif [[ "$1" == "help"* ]]; then
generate_list_cli
# if not sudo
# Runtime "include this script" for USER and development setup condistion
if [[ $EUID != 0 ]]; then
source "$lib_dir/config.ng.runtime.dev.sh" ;
echo "Loaded Develoment
Runtime conditions..." | show_infobox ;

fi
}

tput clear
#
# Check arguments for long flag options
# Help message related to the functions the back end
handle_long_flag(){
if [[ "$1" == "--help" ]]; then
generate_list_run
exit 0 ;
elif [[ "$1" == "--doc" ]]; then
generate_doc
exit 0 ;
fi
# WIP:
if [ "$1" == "--run" ]; then
shift # Shifts the arguments to the left, excluding the first argument ("-r")
group_name="$1" # Takes the first argument as the group name
shift 1 # Shifts the arguments again to exclude the group name

function_name=$(parse_action "$group_name" "$1")
if [ $? -eq 0 ]; then
# Call the function using variable indirection
${function_name}
fi
elif [ "$1" == "--help" ]; then
generate_list_run
exit
elif [ "$1" == "--test" ]; then
check_distro | armbian-interface -o && $1 > /dev/null
fi

}
#
# Check arguments for short flag options
# THe interface help message
handle_short_flag(){
if [ "$1" == "-h" ]; then
generate_help
exit 0 ;
# Generate a text-based user interface
elif [ "$1" == "-t" ] ; then
generate_read ; exit 0 ;
# Generate all doc files
elif [ "$1" == "-d" ] ; then
generate_doc ; exit 0 ;
elif [ "$1" == "-j" ] ; then
generate_json ; exit 0 ;
fi
# Generate the top menu with the modified Object data
while generate_top_menu "$json_data"; do tput clear ; done

}

case "$1" in
*"="*)
# Handle the case where $1 contains "="
handle_no_flag "$@"
;;
*"--"*)
# Handle the case where $1 starts with "--"
handle_long_flag "$@"
;;
*"-"*)
# Handle the case where $1 starts with "-"
handle_short_flag "$1"
;;
*)
handle_no_flag "$@"
# Handle the case where $1 does not match any of the above patterns
# You can add your code here
;;
esac

if [[ -z "$1" ]] ; then
while true; do
generate_tui ;
if [[ "$?" == "0" ]]; then
exit 0
fi
done

fi

#
# Exit the script with a success status code
exit 0
78 changes: 0 additions & 78 deletions bin/configng

This file was deleted.

Loading

0 comments on commit 93e87c6

Please sign in to comment.