summaryrefslogtreecommitdiff
path: root/contrib/scripts/dir2org.zsh
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/scripts/dir2org.zsh')
-rwxr-xr-xcontrib/scripts/dir2org.zsh54
1 files changed, 54 insertions, 0 deletions
diff --git a/contrib/scripts/dir2org.zsh b/contrib/scripts/dir2org.zsh
new file mode 100755
index 0000000..1ea8be4
--- /dev/null
+++ b/contrib/scripts/dir2org.zsh
@@ -0,0 +1,54 @@
+# desc:
+#
+# Output an org compatible structure representing the filesystem from
+# the point passed on the command line (or . by default).
+#
+# options:
+# none
+#
+# usage:
+# dir2org.zsh [DIR]...
+#
+# author:
+# Phil Jackson (phil@shellarchive.co.uk)
+
+set -e
+
+function headline {
+ local depth="${1}"
+ local text="${2}"
+
+ printf "%${depth}s %s" "" | tr ' ' '*'
+ echo " ${text}"
+}
+
+function scan_and_populate {
+ local depth="${1}"
+ local dir="${2}"
+
+ headline ${depth} "${dir}"
+
+ # if there is no files in dir then just move on
+ [[ $(ls "${dir}" | wc -l) -eq 0 ]] && return
+
+ (( depth += 1 ))
+
+ for f in $(ls -d "${dir}"/*); do
+ if [ -d "${f}" ]; then
+ scan_and_populate ${depth} "${f}"
+ else
+ headline ${depth} "[[file://${f}][${${f##*/}%.*}]]"
+ fi
+ done
+
+ (( depth -= 1 ))
+}
+
+function main {
+ local scan_dir="${1:-$(pwd)}"
+ local depth=0
+
+ scan_and_populate ${depth} "${scan_dir}"
+}
+
+main "${@}"