set -Eeuo pipefail
APP_NAME="Listing to File-System converter"
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 ""
}
while [[ $# -gt 0 ]]
do
arg="$1"
shift
case "$arg" in
-o|--output-dir)
output_dir="$1"
shift
;;
-h|--help)
print_help
exit 0
;;
-*) >&2 echo "ERROR: Unknown switch '$arg'"
exit 1
;;
*) POSITIONAL+=("$arg") ;;
esac
done
set -- "${POSITIONAL[@]}"
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