aboutsummaryrefslogtreecommitdiff
path: root/base/.local/bin/textart/rndtxtart
blob: da6ce7d695e432812c6ed2e47e64d026fafbb733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh

have() {
    command -v "$1" >/dev/null 2>&1
}

show_cowsay() {
    if have fortune && have cowsay; then
       # COWDIR=$(cowsay -l | head -n1 | sed -E 's/^Cow files in (.*):$/\1/')
       # COW=$(find "$COWDIR" -name '*.cow' 2>/dev/null | shuf -n1)
        COW=$(cowsay -l | tail -n +2 | tr ' ' '\n' | grep -v '^$' | shuf -n1)
        if have lolcat; then
            fortune | cowsay -f "$COW" | lolcat
        else
            fortune | cowsay -f "$COW"
        fi
    else
        show_textart
    fi
}

show_textart() {
    base_path=$HOME/.local/bin/textart/enabled

    enabled_length=$(ls $base_path | wc -l)
    random=$(od -vAn -N4 -t u4 < /dev/urandom)
    selected=$(( $random % $enabled_length ))
    [ "$selected" -eq "0" ] && selected=1

    script=$(ls $base_path | head -n $selected | tail -n 1)

    $base_path/$script
}

# 50/50: shuf -i 0-1 -n1 devuelve 0 ó 1 al azar
if [ "$(shuf -i 0-1 -n1)" -eq 0 ]; then
    show_cowsay
else
    show_textart
fi
echo ""