#!/usr/bin/env bash # SPDX-FileCopyrightText: 2022 Robin Vobruba # SPDX-License-Identifier: AGPL-3.0-or-later # # See the output of "$0 -h" for details. # Exit immediately on each error and unset variable; # see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ set -Eeuo pipefail #set -Eeu APP_NAME="Listing to File-System converter" # parameters output_dir="$(pwd)" function print_help() { script_name="$(basename "$0")" echo -e "" echo -e "$APP_NAME - Given a file listing, creates the actual file-system representation of it." echo -e "The input file-listing format has to follow these rules:" echo -e "* one file/dir per line" echo -e "* full paths, relative to the output-dir" echo -e "* each line indicating a dir needs to end with a '/'" echo -e "* empty lines and lines starting with '#' or ';', are ignored" echo -e "* any other line indicates a file" echo -e "" echo -e "Usage:" echo -e "\t$script_name [OPTION...]" echo -e "Options:" echo -e "\t-o, --output-dir" echo -e "\t\tAdditionally prints a tree view of the created dir structure" echo -e "\t-h, --help" echo -e "\t\tPrint this usage help and exit" echo -e "" } # Process command line arguments while [[ $# -gt 0 ]] do arg="$1" shift # $2 -> $1, $3 -> $2, ... case "$arg" in -o|--output-dir) output_dir="$1" shift ;; -h|--help) print_help exit 0 ;; -*) # unknown switch >&2 echo "ERROR: Unknown switch '$arg'" exit 1 ;; *) # non-switch POSITIONAL+=("$arg") # save it in an array for later ;; esac done set -- "${POSITIONAL[@]}" # restore positional parameters if ! [ -d "$output_dir" ] || [ -n "$(/usr/bin/ls -A "$output_dir")" ] then >&2 echo "The output dir is not an existing, empty dir: '$output_dir'" exit 1 fi while IFS= read -r line do if [ -z "$line" ] || [[ $line =~ ^[#\;].* ]] then continue fi if [[ $line =~ (^\|/)\.\.(/\|$) ]] then >&2 echo "Invalid sequence '..' found in: '$line'" exit 1 fi if [[ $line =~ ^/ ]] then >&2 echo "Please use relative listing paths; found: '$line'" exit 1 fi if [[ $line =~ .*/$ ]] then mkdir -p "$output_dir/$line" else parent_dir="$(dirname "$line")" mkdir -p "$output_dir/$parent_dir" touch "$output_dir/$line" fi done