From a5f136faab21e6d2d12e3bee59ea61f568c09f09 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 16 Jan 2024 12:19:00 -0700 Subject: [PATCH 1/5] build-index changed to #!/bin/sh for POSIX shell --- build-index.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-index.sh b/build-index.sh index c23e96e..aa6e60c 100755 --- a/build-index.sh +++ b/build-index.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # defin the target dir parent=$(dirname "${BASH_SOURCE[0]}") From 07a41820ca9371d2870824475534c1434a267e54 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 16 Jan 2024 12:21:44 -0700 Subject: [PATCH 2/5] useless use of backticks (https://porkmail.org/era/unix/award#backticks) --- build-index.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-index.sh b/build-index.sh index aa6e60c..02cb1f9 100755 --- a/build-index.sh +++ b/build-index.sh @@ -5,7 +5,7 @@ parent=$(dirname "${BASH_SOURCE[0]}") cd $parent; cd ../../../ directory=$(pwd -P) -today=`date "+%Y-%m-%d"` +today="$(date "+%Y-%m-%d")" echo -e "Date: $today\n" From 6ea0433619275c80fa7e2f8b3be8fdaf2dc6176b Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 16 Jan 2024 12:36:18 -0700 Subject: [PATCH 3/5] echo(1) -> printf(1p): portability and nonstandard usage --- build-index.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/build-index.sh b/build-index.sh index 02cb1f9..35c5ec1 100755 --- a/build-index.sh +++ b/build-index.sh @@ -7,29 +7,29 @@ directory=$(pwd -P) today="$(date "+%Y-%m-%d")" -echo -e "Date: $today\n" +printf "Date: %s\n" "$today" for dirs in "$directory"/*; do - string="---\n" + string="$(printf "---\n")" area=$(basename "${dirs}") if [ "$area" != "System Volume Information" ]; then - string+="title: $area\n" - string+="date: $today\n" - string+="---\n\n" + string="$(printf "%stitle: %s\n" "$string" "$area")" + string="$(printf "%sdate: %s\n" "$string" "$today")" + string="$(printf "%s---\n\n" "$string")" for cats in "$dirs"/*; do category=$(basename "${cats}") - string+="# $category\n\n" + string="$(printf "%s# %s\n\n" "$string" "$category")" for ids in "$cats"/*; do ident=$(basename "${ids}") - string+="* $ident\n" + string="$(printf "%s* %s\n" "$string" "$ident")" done - string+="\n" + string="$(printf "%s\n" "$string")" done savedir="$directory/00-09 Index/00 Index/00.01 Index/" fname="${area:0:5}.md" fullpath="$savedir$fname" - echo $fullpath - echo -e $string > "$fullpath" + printf '%s\n' "$fullpath" + printf '%s' "$string" > "$fullpath" fi done From aba50c2843e745a9e2b1c5e8483951543069dc4b Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 16 Jan 2024 12:52:26 -0700 Subject: [PATCH 4/5] formatting, quoting, & typo --- build-index.sh | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/build-index.sh b/build-index.sh index 35c5ec1..41c30d7 100755 --- a/build-index.sh +++ b/build-index.sh @@ -1,9 +1,9 @@ #!/bin/sh -# defin the target dir -parent=$(dirname "${BASH_SOURCE[0]}") -cd $parent; cd ../../../ -directory=$(pwd -P) +# define the target dir +parent="$(dirname "${BASH_SOURCE[0]}")" +cd "$parent"; cd ../../../ +directory="$(pwd -P)" today="$(date "+%Y-%m-%d")" @@ -11,25 +11,30 @@ printf "Date: %s\n" "$today" for dirs in "$directory"/*; do string="$(printf "---\n")" - area=$(basename "${dirs}") + area="$(basename "${dirs}")" if [ "$area" != "System Volume Information" ]; then string="$(printf "%stitle: %s\n" "$string" "$area")" string="$(printf "%sdate: %s\n" "$string" "$today")" string="$(printf "%s---\n\n" "$string")" + for cats in "$dirs"/*; do - category=$(basename "${cats}") + category="$(basename "$cats")" string="$(printf "%s# %s\n\n" "$string" "$category")" + for ids in "$cats"/*; do - ident=$(basename "${ids}") + ident="$(basename "$ids")" string="$(printf "%s* %s\n" "$string" "$ident")" done + string="$(printf "%s\n" "$string")" done - savedir="$directory/00-09 Index/00 Index/00.01 Index/" - fname="${area:0:5}.md" - fullpath="$savedir$fname" - printf '%s\n' "$fullpath" - printf '%s' "$string" > "$fullpath" + + savedir="$directory/00-09 Index/00 Index/00.01 Index/" + fname="${area:0:5}.md" + fullpath="$savedir$fname" + + printf '%s\n' "$fullpath" + printf '%s' "$string" > "$fullpath" fi done From 49a3d524d7ccf4aa346401d9c0f1cc898fe95252 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 16 Jan 2024 15:05:12 -0700 Subject: [PATCH 5/5] rewrote to use POSIX shell features --- CHANGELOG | 5 +++ build-index.sh | 97 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 90e94b4..a7e016f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,10 @@ # Changelog +## 2024-12-16 +- Rewrote build-index.sh to be POSIX-compliant +- build-index.sh now takes two arguments. The first is the root directory of the +JD system, and the second is the output directory. + ## 2023-12-17 Repository created diff --git a/build-index.sh b/build-index.sh index 41c30d7..b2378f4 100755 --- a/build-index.sh +++ b/build-index.sh @@ -1,40 +1,83 @@ #!/bin/sh -# define the target dir -parent="$(dirname "${BASH_SOURCE[0]}")" -cd "$parent"; cd ../../../ -directory="$(pwd -P)" +# MIT License +# +# Copyright (c) 2023 Cordelya Sharpe +# Copyright (c) 2024 Emma Tebibyte +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. -today="$(date "+%Y-%m-%d")" +set -e -printf "Date: %s\n" "$today" +test -n "$DEBUG" && set -x -for dirs in "$directory"/*; do - string="$(printf "---\n")" - area="$(basename "${dirs}")" - if [ "$area" != "System Volume Information" ]; then +if test -z "$2"; then + if test -z "$1"; then + # output to and read system from current dir if $1 and $2 are not set + out="$(pwd -P)" + root="$out" + else + # if $1 is set, that is the system root + root="$1" + fi +else + # if both $1 and $2 are set, they are the root and output dir + root="$1" + out="$2" +fi - string="$(printf "%stitle: %s\n" "$string" "$area")" - string="$(printf "%sdate: %s\n" "$string" "$today")" - string="$(printf "%s---\n\n" "$string")" +date="$(date "+%Y-%m-%d")" - for cats in "$dirs"/*; do - category="$(basename "$cats")" - string="$(printf "%s# %s\n\n" "$string" "$category")" +# for every area dir in the root, get the basename of the area +for dir in "$root"/*; do + area="$(basename "$dir")" - for ids in "$cats"/*; do - ident="$(basename "$ids")" - string="$(printf "%s* %s\n" "$string" "$ident")" + # only include jd files + if test -n "$(printf '%s\n' "$area" | sed -n '/^[0-9][0-9]-.*/p')" + then + # set the filename to the area number for the filename of the index + fullpath="$out/$(printf '%s\n' "$area" | dd bs=5 count=1 2>/dev/null).md" + + # if the file already exists, back it up + if test -f "$fullpath"; then + mv "$fullpath" "$fullpath.old" + fi + + printf '%s: %s: Saving index as %s.\n' "$0" "$area" "$fullpath" 1>&2 + + # add front matter to markdown file + printf -- "---\ntitle: %s\ndate: %s\n---\n" "$area" "$date" >>"$fullpath" + + # iterate through catergories + for c in "$dir"/*; do + category="$(basename "$c")" + # add category as a header to the output file + printf "# %s\n\n" "$category" >>"$fullpath" + + # iterate through contents of categories + for i in "$c"/*; do + id="$(basename "$i")" + # add each content item to the output file + printf "* %s\n" "$id" >>"$fullpath" done - string="$(printf "%s\n" "$string")" + printf '\n' >>"$fullpath" done - - savedir="$directory/00-09 Index/00 Index/00.01 Index/" - fname="${area:0:5}.md" - fullpath="$savedir$fname" - - printf '%s\n' "$fullpath" - printf '%s' "$string" > "$fullpath" fi done