|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +source scripts/languages.bash |
| 4 | + |
| 5 | +# Load environment variables from .env file |
| 6 | +function load_env_vars() { |
| 7 | + if [ -f .env ]; then |
| 8 | + # shellcheck disable=SC2046 |
| 9 | + export echo $(sed <.env 's/#.*//g' | xargs | envsubst) |
| 10 | + |
| 11 | + # Check if required variables are set and not equal to default values |
| 12 | + if [ "$NICKNAME" = "your_nickname" ] || [ "$LANGUAGE" = "choose_your_language" ]; then |
| 13 | + echo "Error: Required environment variables are set to default values." |
| 14 | + echo "Please update NICKNAME and LANGUAGE in the .env file with appropriate values." |
| 15 | + exit 1 |
| 16 | + fi |
| 17 | + |
| 18 | + # Check if the specified language is valid |
| 19 | + |
| 20 | + if [[ ! "${!language_extensions[@]}" =~ $LANGUAGE ]]; then |
| 21 | + echo "Error: Invalid language specified in the .env file." |
| 22 | + echo "Please set LANGUAGE to one of the following valid languages:" |
| 23 | + echo "${!language_extensions[@]}" |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +# Check if Bash version meets the minimum requirement |
| 30 | +function check_bash_version() { |
| 31 | + local required_version=$1 |
| 32 | + local bash_version |
| 33 | + bash_version=$(bash --version | head -n1 | awk '{print $4}' | sed 's/\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/') |
| 34 | + |
| 35 | + if [[ $bash_version =~ ^([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then |
| 36 | + local major=${BASH_REMATCH[1]} |
| 37 | + local minor=${BASH_REMATCH[2]} |
| 38 | + local patch=${BASH_REMATCH[3]} |
| 39 | + |
| 40 | + IFS='.' read -r -a required_parts <<<"$required_version" |
| 41 | + local required_major=${required_parts[0]} |
| 42 | + local required_minor=${required_parts[1]} |
| 43 | + local required_patch=${required_parts[2]} |
| 44 | + |
| 45 | + if ((major > required_major || (\ |
| 46 | + major == required_major && minor > required_minor) || (\ |
| 47 | + major == required_major && minor == required_minor && patch >= required_patch))); then |
| 48 | + return 0 |
| 49 | + else |
| 50 | + return 1 |
| 51 | + fi |
| 52 | + else |
| 53 | + return 1 |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +# Check if a command is installed |
| 58 | +function check_command() { |
| 59 | + local command="$1" |
| 60 | + if ! command -v "$command" &>/dev/null; then |
| 61 | + echo "The $command command is not installed." |
| 62 | + read -r -p "Do you want to install $command? (Y/n): " install_command |
| 63 | + case "$install_command" in |
| 64 | + [nN] | [nN][oO]) |
| 65 | + echo "Installation of $command has been rejected. Exiting the script." |
| 66 | + exit 1 |
| 67 | + ;; |
| 68 | + *) |
| 69 | + echo "Proceeding with the installation of $command..." |
| 70 | + brew install "$command" |
| 71 | + ;; |
| 72 | + esac |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +# Generates the solution code template with question details and author information |
| 77 | +function make_solution_code() { |
| 78 | + local question_id=$1 |
| 79 | + local question_name=$2 |
| 80 | + local question_url=$3 |
| 81 | + local code=$4 |
| 82 | + local comment=${language_comments[$LANGUAGE]} |
| 83 | + local nickname |
| 84 | + if [ -n "$NICKNAME" ]; then |
| 85 | + nickname=$NICKNAME |
| 86 | + else |
| 87 | + nickname=Unknown |
| 88 | + fi |
| 89 | + local content |
| 90 | + content=$( |
| 91 | + cat <<EOF |
| 92 | +${comment} |
| 93 | +${comment}$question_id. $question_name |
| 94 | +${comment}$question_url |
| 95 | +${comment}Dale-Study |
| 96 | +${comment} |
| 97 | +${comment}Created by $nickname on $(date "+%Y/%m/%d"). |
| 98 | +${comment} |
| 99 | +
|
| 100 | +$code |
| 101 | +
|
| 102 | +EOF |
| 103 | + ) |
| 104 | + echo "$content" |
| 105 | +} |
| 106 | + |
| 107 | +# Saves the solution code to a file in the appropriate directory based on the question slug and author's nickname |
| 108 | +function save_file() { |
| 109 | + local DIR |
| 110 | + local title_slug="$1" |
| 111 | + local content="$2" |
| 112 | + local nickname |
| 113 | + local language_extension |
| 114 | + local solution_folder |
| 115 | + local solution_file |
| 116 | + |
| 117 | + if [ -n "$NICKNAME" ]; then |
| 118 | + nickname=$NICKNAME |
| 119 | + else |
| 120 | + nickname=Unknown |
| 121 | + fi |
| 122 | + |
| 123 | + DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" |
| 124 | + language_extension=${language_extensions[$LANGUAGE]} |
| 125 | + |
| 126 | + solution_folder="$DIR/../$title_slug" |
| 127 | + mkdir -p "$solution_folder" |
| 128 | + |
| 129 | + solution_file="$solution_folder/$nickname.$language_extension" |
| 130 | + echo "$content" >"$solution_file" |
| 131 | + echo "File creation completed" |
| 132 | +} |
0 commit comments