cardsh/mkdeck

125 lines
3.2 KiB
Plaintext
Raw Normal View History

2024-04-25 16:21:02 -05:00
#!/bin/sh
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
# Name:
# Desc:
# Reqs:
# Date:
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
2024-04-25 17:52:46 -05:00
FRENCH_CARDS="♣♚ ♣♛ ♣♝ ♠♚ ♠♛ ♠♝ ♢♔ ♢♕ ♢♗ ♡♔ ♡♕ ♡♗ 🃏 🃏"
2024-04-25 16:21:02 -05:00
FRENCH_SUITS="♣ ♢ ♡ ♠"
FRENCH_SUIT_CARDS="A 2 3 4 5 6 7 8 9 10"
2024-04-25 17:52:46 -05:00
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 decks 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.'
}
2024-04-25 16:21:02 -05:00
shell_sanitize() {
local str="$1"
echo "$1" \
| tr -d '$(){}`'
}
suit_cards() {
local name="$(shell_sanitize "$1")"
2024-04-25 17:52:46 -05:00
local suits="$(eval "echo \$${name}_SUITS")"
2024-04-25 16:21:02 -05:00
local suit_cards="$(eval "echo \$${name}_SUIT_CARDS")"
2024-04-25 16:21:02 -05:00
for suit in $suits; do
for suit_card in $suit_cards; do
printf "${suit}${suit_card} "
done
done
2024-04-25 17:52:46 -05:00
eval "echo \$${name}_CARDS"
2024-04-25 16:21:02 -05:00
}
mkdeck_dir() {
2024-04-25 16:21:02 -05:00
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:"
2024-04-25 17:52:46 -05:00
1>&2 echo "${deck_name}_CARDS, ${deck_name}_SUITS, and ${deck_name}_SUIT_CARDS."
exit 3
2024-04-25 16:21:02 -05:00
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"
}
2024-04-25 17:52:46 -05:00
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"