#!/bin/bash # gen-catalog - Generate SFCC (Demandware) catalog XML for testing # # Usage: # gen-catalog [catalog_id] # gen-catalog -h # # Options: # base_count Number of base products to generate (positive integer) # variants_per_base Number of variants per base product (positive integer) # catalog_id Catalog ID (default: test-catalog) # -h, --help Show help message _gen_catalog() ( local SCRIPT_NAME; SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" case "${BASH_SOURCE[0]}" in /dev/*|/proc/*) SCRIPT_NAME="" ;; esac case "$SCRIPT_NAME" in ""|bash|sh|zsh|dash) SCRIPT_NAME="gen-catalog" ;; esac _show_help() { local s; [ -t 1 ] && s=$'\033[4m' local r; [ -t 1 ] && r=$'\033[24m' echo "NAME" echo " $SCRIPT_NAME - generate SFCC catalog XML" echo "SYNOPSIS" echo " $SCRIPT_NAME ${s}base_count${r} ${s}variants_per_base${r} [${s}catalog_id${r}]" echo " $SCRIPT_NAME -h" echo "DESCRIPTION" echo " Generates a Demandware/SFCC catalog XML document with the specified number" echo " of base products, each with the given number of variants. Output is written" echo " to stdout." echo "OPTIONS" echo " ${s}base_count${r} Number of base products to generate" echo " ${s}variants_per_base${r} Number of variants per base product" echo " ${s}catalog_id${r} Catalog ID (default: test-catalog)" echo " -h, --help Show this help message" echo "EXIT STATUS" echo " 0 Success" echo " 2 Usage error (missing or non-positive-integer arg)" } _error() { echo "[ERR][$SCRIPT_NAME] $*" >&2; } main() { local base_count="$1" local variants_per_base="$2" local catalog_id="$3" if [ ! "$base_count" ] || [ ! "$variants_per_base" ]; then _error "Must provide BASE_COUNT and VARIANTS_PER_BASE. Run \`$SCRIPT_NAME -h\` for usage" return 2 fi case "$base_count" in *[!0-9]*) _error "BASE_COUNT must be a positive integer" return 2 ;; esac case "$variants_per_base" in *[!0-9]*) _error "VARIANTS_PER_BASE must be a positive integer" return 2 ;; esac if [ "$base_count" -le 0 ] || [ "$variants_per_base" -le 0 ]; then _error "BASE_COUNT and VARIANTS_PER_BASE must be greater than zero" return 2 fi if [ ! "$catalog_id" ]; then catalog_id="test-catalog" fi print_header "$catalog_id" print_all_in_order "$base_count" "$variants_per_base" print_footer } print_header() { local catalog_id="$1" echo '' echo '" echo '
' } print_footer() { echo '' } print_all_in_order() { local base_count="$1" local variants_per_base="$2" _print_base() { local base_id="$1" local variants_per_base="$2" echo " " echo " $base_id" echo ' ' echo ' ' local i=1 while [ "$i" -le "$variants_per_base" ]; do local num num="$(printf '%03d' "$i")" local vid="$base_id-VAR-$num" if [ "$i" -eq 1 ]; then echo " " else echo " " fi i=$((i + 1)) done echo ' ' echo ' ' echo ' ' } _print_variants() { local base_id="$1" local variants_per_base="$2" local i=1 while [ "$i" -le "$variants_per_base" ]; do local num num="$(printf '%03d' "$i")" local vid="$base_id-VAR-$num" echo " " echo " $base_id Variant $num" echo ' ' i=$((i + 1)) done } # Ordered output: # BASE1 # BASE1 variants # BASE2 # BASE2 variants local b=1 while [ "$b" -le "$base_count" ]; do local base_id="BASE$b" _print_base "$base_id" "$variants_per_base" _print_variants "$base_id" "$variants_per_base" b=$((b + 1)) done } case "$1" in -h|--help) _show_help; return 0 ;; esac main "$@" ) _gen_catalog "$@" __gen_catalog_rc=$? unset -f _gen_catalog if [ -n "${BASH_SOURCE[0]}" ] && [ "${BASH_SOURCE[0]}" != "$0" ]; then eval "unset __gen_catalog_rc; return $__gen_catalog_rc" fi eval "unset __gen_catalog_rc; exit $__gen_catalog_rc"