#!/bin/bash # SYSTEM: CONFIG New # Edit System Configuration (.json) file # Read input read -p "New Profile Name?: " NEW_PROFILE_NAME # Input validate (check if empty) if [[ -z "$NEW_PROFILE_NAME" ]] then error_message "[n] Config (new): Input blank. No new file created." echo -e "" # Input validate (check for alphanum & "-_ "), convert spaces to underscores elif [[ "$NEW_PROFILE_NAME" =~ [^a-zA-Z0-9_-] ]] then error_message "[n] Config (new): Filenames are alphanum only (no spaces). No file created." echo -e "" # Proceed with profile creation else # Check if name exists, and if so mark flag PROFILE_EXISTS_FLAG="false" for NEW_NAME_CHECK_LOOPED in "${CONFIG_PROFILES_ARRAY[@]}" do if [[ "${NEW_NAME_CHECK_LOOPED}" == ${NEW_PROFILE_NAME} ]] then PROFILE_EXISTS_FLAG="true" fi done # Perform final action based on flag if [[ $PROFILE_EXISTS_FLAG == "true" ]] # If profile exists then error_message "[n] Config (new): config profile named \"$NEW_PROFILE_NAME\" already exist." echo -e"" # If profile doesn't exist else sudo cp "$CONFIG_TEMPLATE_DIRECTORY/config-sample.json" "$CONFIG_DIRECTORY/config-$NEW_PROFILE_NAME.json" TIMESTAMP=$(date +%Y-%m-%d" ("%H"h "%M"m "%S"s)") sed -i "s/%%%%config_name%%%%/$NEW_PROFILE_NAME/" "$CONFIG_DIRECTORY/config-$NEW_PROFILE_NAME.json" sed -i "s/%%%%creation_date%%%%/$TIMESTAMP/" "$CONFIG_DIRECTORY/config-$NEW_PROFILE_NAME.json" sed -i "s/%%%%last_modified%%%%/$TIMESTAMP/" "$CONFIG_DIRECTORY/config-$NEW_PROFILE_NAME.json" confirmation_message "[n] Config (new): Blank config profile \"$NEW_PROFILE_NAME\" created." echo -e "" fi # Unset Flag unset PROFILE_EXISTS_FLAG fi