diff options
| Commit | 9506054826830e9424fbc3d3fdab312b63037663 (patch) | |
|---|---|---|
| Author | 2023-10-17 13:50:57 +0300 | |
| Committer | 2023-10-17 13:50:57 +0300 | |
| Files | e03392da5ffe11b30e2dbf2346106465b0a00450 | |
| csg-9506054826830e9424fbc3d3fdab312b63037663.tar csg-9506054826830e9424fbc3d3fdab312b63037663.tar.gz csg-9506054826830e9424fbc3d3fdab312b63037663.zip | ||
| Parent | 1f7519c430c8f29d8daf926c0c040f6a404d1349 (diff) | |
(p1,p2,p3,p4) Reorganized preprocessing steps, divided them into parts, added more checks, made certain error messages more helpful
1 files changed, 47 insertions, 38 deletions
| -rwxr-xr-x | csg | 85 |
@@ -1,26 +1,7 @@ #!/bin/bash -### Helper functions - -isCommand() { - command -v "$1" &> /dev/null -} - -overwriteLine() { - printf "\e[1A\e[K$1\n" -} - -_dur=0 -startTimer() { - _dur="$(date +%s%N)" - printf "$1\n" -} -endTimer() { - _dur="$(echo "scale=4 ; ($(date +%s%N) - $_dur) / 1000000000" | bc -l)" - printf "\e[1A\e[K$1 %.3fs\n" $_dur -} - -### Parameters and variables +### Preprocessing 1 +### Help message csg_help() { echo 'Usage: csg <source_dir> <output_dir> <title_format> [<scripts_dir>] @@ -38,11 +19,7 @@ Variables: CSG_AWK CSG_MD_TO_HTML CSG_TIMESTAMPS_FILE - CSG_TEMP_DIR - -Exit status: - 1 Missing dependency - 2 Bad file type or missing read/write permissions + CSG_TMP_DIR Dependencies: bash @@ -52,19 +29,42 @@ Dependencies: [ -z "$1" -o "$1" = '-h' -o "$1" = '-?' -o "$1" = '--help' ] && csg_help 0 +### Preprocessing 2 +### Script parameters + SOURCE="$1" -[ ! -d "$SOURCE" ] && echo "[Error] Passed $SOURCE isn't a directory or doesn't exist!" \ - && csg_help 2 -[ ! -r "$SOURCE" ] && echo "[Error] No read access to $SOURCE!" \ +[ ! -d "$SOURCE" ] && echo "[Error] $SOURCE (<source_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 +[ ! -r "$SOURCE" ] && echo "[Error] No read access to $SOURCE (<source_dir>)!" \ && csg_help 2 OUTPUT="$(realpath "$2")" +[ ! -d "$OUTPUT/.." ] && echo "[Error] Parent directory of $2 (<output_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 +[ ! -w "$OUTPUT/.." ] && echo "[Error] No write access to parent directory of $2 (<output_dir>)!" \ + && csg_help 2 + TITLEF="$3" AWKSCRIPTS="$4" -[ ! -d "$AWKSCRIPTS" ] && echo '[Error] Passed <scripts_dir> isn'\''t a directory or doesn'\''t exist!' \ +[ -n "$AWKSCRIPTS" -a ! -d "$AWKSCRIPTS" ] && echo "[Error] $AWKSCRIPTS (<scripts_dir>) isn't a directory or doesn't exist!" \ + && csg_help 1 + +### Preprocessing 3 +### "Global" variables + +: ${CSG_TIMESTAMP_FILE:="$(realpath ./timestamps.txt)"} + +: ${CSG_TMP_DIR:='/tmp'} +[ ! -d "$CSG_TMP_DIR" ] && echo "[Error] $CSG_TMP_DIR (\$CSG_TMP_DIR) doesn't exist!" \ + && csg_help 1 +[ ! -r "$CSG_TMP_DIR" -o ! -w "$CSG_TMP_DIR" ] && echo "[Error] No read or write permissions for $CSG_TMP_DIR (\$CSG_TMP_DIR)!" \ && csg_help 2 +isCommand() { + command -v "$1" &> /dev/null +} + if [ -n "$CSG_AWK" ] then : elif isCommand 'gawk' @@ -72,7 +72,7 @@ then CSG_AWK='gawk --posix' else echo '[Error] Couldn'\''t find an awk implementation!' - csg_help 1 + csg_help 3 fi if [ -n "$CSG_MD_TO_HTML" ] @@ -86,16 +86,25 @@ then --parse-no-autolink" else echo '[Error] Couldn'\''t find a Markdown to HTML convertion program!' - csg_help 1 + csg_help 3 fi -: ${CSG_TIMESTAMP_FILE:="$(realpath ./timestamps.txt)"} +### Preprocessing 4 +### Timer/status function definitions -: ${CSG_TMP_DIR:='/tmp'} -[ ! -d "$CSG_TMP_DIR" ] && echo "[Error] Temporary directory $CSG_TMP_DIR doesn't exist!" \ - && csg_help 2 -[ ! -r "$CSG_TMP_DIR" -o ! -w "$CSG_TMP_DIR" ] && echo "[Error] No read or write permissions to temporary directory $CSG_TMP_DIR!" \ - && csg_help 2 +overwriteLine() { + printf "\e[1A\e[K$1\n" +} + +_dur=0 +startTimer() { + _dur="$(date +%s%N)" + printf "$1\n" +} +endTimer() { + _dur="$(echo "scale=4 ; ($(date +%s%N) - $_dur) / 1000000000" | bc -l)" + printf "\e[1A\e[K$1 %.3fs\n" $_dur +} ### Stage 0 ### Create timestamps.txt file, if necessary |
