125 lines
3.2 KiB
Bash
Executable File
125 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
||
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||
# Name:
|
||
# Desc:
|
||
# Reqs:
|
||
# Date:
|
||
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||
|
||
FRENCH_CARDS="♣♚ ♣♛ ♣♝ ♠♚ ♠♛ ♠♝ ♢♔ ♢♕ ♢♗ ♡♔ ♡♕ ♡♗ 🃏 🃏"
|
||
FRENCH_SUITS="♣ ♢ ♡ ♠"
|
||
FRENCH_SUIT_CARDS="A 2 3 4 5 6 7 8 9 10"
|
||
|
||
|
||
usage() {
|
||
echo "usage: $(basename "$0") [-h] [-d DECK_PREFIX] DIRECTORY"
|
||
echo
|
||
echo "$(basename "$0") creates a directory representing a card-deck."
|
||
echo 'That is, it creates empty files representing the decks’ cards,'
|
||
echo 'with the files’ names corresponding to their card in the deck.'
|
||
echo
|
||
echo ' -d specify the deck-prefix to use'
|
||
echo ' -h print this message and exit'
|
||
echo
|
||
echo 'A deck-prefix is the name of the deck, and determines the'
|
||
echo 'variables that will be used to determine the deck’s cards.'
|
||
echo 'For instance, if the deck-prefix is FRENCH — the deck'
|
||
echo 'hard-coded echo "into $(basename "$0") — then the variables'
|
||
echo 'FRENCH_CARDS, FRENCH_SUITS, and FRENCH_SUIT_CARDS will be used.'
|
||
echo
|
||
echo '$NAME_CARDS contains a space-delimited list of strings of raw'
|
||
echo 'card-names, like: “♣♚ ♢♔ 🃏”'
|
||
echo '$NAME_SUITS contains a list of suits, like: “♣ ♡”'
|
||
echo '$NAME_SUIT_CARDS containts a list of cards each suit has, for'
|
||
echo 'example: “1 2 3 4”'
|
||
echo
|
||
echo 'You can define a deck simply using $NAME_CARDS, or you can use'
|
||
echo '$NAME_SUITS and $NAME_SUITS_CARDS to help reduce repetiton on'
|
||
echo 'your part. Substitute “NAME” with your preferred deck-name.'
|
||
}
|
||
|
||
|
||
shell_sanitize() {
|
||
local str="$1"
|
||
echo "$1" \
|
||
| tr -d '$(){}`'
|
||
}
|
||
|
||
|
||
suit_cards() {
|
||
local name="$(shell_sanitize "$1")"
|
||
local suits="$(eval "echo \$${name}_SUITS")"
|
||
local suit_cards="$(eval "echo \$${name}_SUIT_CARDS")"
|
||
|
||
for suit in $suits; do
|
||
for suit_card in $suit_cards; do
|
||
printf "${suit}${suit_card} "
|
||
done
|
||
done
|
||
|
||
eval "echo \$${name}_CARDS"
|
||
}
|
||
|
||
|
||
mkdeck_dir() {
|
||
local dir="$1"
|
||
|
||
if test ! -d "$dir"; then
|
||
mkdir "$dir"
|
||
fi
|
||
if test ! -d "$dir"; then
|
||
1>&2 echo "Failed to create deck directory at $dir."
|
||
exit 2
|
||
fi
|
||
}
|
||
|
||
|
||
mkdeck_cards() {
|
||
local dir="$1"
|
||
local deck_name="$2"
|
||
local cards="$(suit_cards "$deck_name")"
|
||
|
||
if test -z "$cards"; then
|
||
1>&2 echo "No cards found for deck $deck_name. Check your environment variables:"
|
||
1>&2 echo "${deck_name}_CARDS, ${deck_name}_SUITS, and ${deck_name}_SUIT_CARDS."
|
||
exit 3
|
||
fi
|
||
|
||
for card in $cards; do
|
||
touch "${dir}/${card}"
|
||
done
|
||
}
|
||
|
||
|
||
mkdeck() {
|
||
local dir="$1"
|
||
local deck_name="$2"
|
||
mkdeck_dir "$dir"
|
||
mkdeck_cards "$dir" "$deck_name"
|
||
}
|
||
|
||
|
||
DECK="$FRENCH"
|
||
while getopts 'hd:' arg; do
|
||
case $arg in
|
||
h)
|
||
usage
|
||
exit 0
|
||
;;
|
||
d)
|
||
DECK="$OPTARG"
|
||
;;
|
||
esac
|
||
done
|
||
|
||
shift $((OPTIND-1))
|
||
|
||
CARD_DIR="$1"
|
||
|
||
if test -z "$CARD_DIR"; then
|
||
usage 1>&2
|
||
exit 4
|
||
fi
|
||
|
||
mkdeck "$CARD_DIR" "$DECK"
|