#!/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="definition (CSV) to file listing converter" # parameters function print_help() { script_name="$(basename "$0")" echo -e "" echo -e "$APP_NAME - Given a definition.csv (as found in mod/*/definition.csv')" echo -e "generates a simple file listing." echo -e "Basically, it just extracts the first column without the header" echo -e "The input files first column 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-h, --help" echo -e "\t\tPrint this usage help and exit" echo -e "Examples:" echo -e "\t$script_name mod/unixish/definition.csv" echo -e "" } # Process command line arguments while [[ $# -gt 0 ]] do arg="$1" shift # $2 -> $1, $3 -> $2, ... case "$arg" in -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 [ -z "${1:-}" ] then >&2 echo "ERROR: Please supply path to input file (definition.csv)." exit 1 fi input_file="$1" tail -n +2 "$input_file" | while IFS= read -r line do if [ -z "$line" ] || [[ $line =~ ^[#\;].* ]] then continue fi if [[ $line =~ ^\" ]] then echo "$line" | sed -e 's|^"||' -e 's|",.*||' else echo "${line%%,*}" fi done