#!/bin/sh

PATH=/live/bin:/bin
dir=/live/custom/tao

rand_hex() {
    dd if=/dev/urandom count=16 bs=1 2>/dev/null \
        | md5sum | sed -r "s/(.{8}).*/0x\1/"
}

rand() {
    echo $(($(rand_hex) % $1))
}

main() {
    cnt=0
    idx=$(head -n $(rand 62) $dir/tao.idx | tail -n1)
    : ${idx:=0}

    IFS=''
    tail -n $idx $dir/tao.txt | while read -r line; do
        case $line in
            [1-9]*|BOOK*|\([IV]*) cnt=$((cnt + 1))
        esac
        [ $cnt -gt 1 ] && break
        echo "$line"
    done
}

# tac tao.txt | egrep -n "^([1-9]|BOOK|\([IV])" | cut -d: -f1 > tao.idx

main

