#! /bin/bash

# copyright (C)  Ly(r)icLy  2024

# this month's edition of   taswelll impersonation   is funded
# by viewers like you

# as always,   taswelll impersonation   is brought to you by:
# the Ly(r)icLy LLC. Co. MK. AB conglomorate company, Inc.  (all rights restricted)


# no waranty is provided; there is no guarantee of fitness for any purpose.
#     ^^^  im not capitalizing all that shit like normal licenses do



nl=$'\n'
tab=$'\t'

ddefault_flock_sl=0.05

#comment these out if your machine has a builtin flock and flunlock!!
flock(){
    if [ ${doverride_flock_sl} ]; then
        sleep ${doverride_flock_sl}
        return
    fi
    #flock isnt portable in bash, so   taswelll.ai by LyRicLY   will just
    #handwave it by sleeping a bit
    if [ ${2} ]; then
        sleep $2
    else
        sleep $ddefault_flock_sl
    fi
}
flunlock(){
    #funlock isnt portable in bash, so   taswelll.ai by LyRicLY   will just
    #handwave it by sleeping a bit
    if [ $2 ]; then
        sleep $2
    else
        sleep $ddefault_flock_sl
    fi
}


join(){
    v_joined="${2}"
    for ((L = 3; L <= $#; L++)); do
        v_joined="${v_joined}${1}${!L}"
    done
    printf "%s" "${v_joined}"
}

readcmd(){
    flock   ${!2}
    v_cmdoutp=""
    while IFS= read -r outline; do
        v_cmdoutp=$(join "${nl}${tab}" "${v_cmdoutp}" "${outline}")
    done < <(eval $1)
    flunlock ${!2}
    printf "%s" "${v_cmdoutp}"
}

saychar(){
    flock    ${____filestruct__STDOUT_}
    i="$2"; echo -n "${1:i:1}"
    flunlock ${____filestruct__STDOUT_}
}
saychars(){
    # sometimes flock fails here??but it works when twice
    flock    ${____filestruct__STDOUT_}; flock ${____filestruct__STDOUT_}
    for ((i = 0; i < ${#1}; i++)); do
        saychar "$1" $i
    done
    flunlock ${____filestruct__STDOUT_}
}
sayline(){
    saychars "${1}"; saychar $'\n'
}
saylines(){
    flock    ${____filestruct__STDOUT_} 0.4
    for ((L = 1; L <= $#; L++)); do
        sayline "${!L}"
    done
    flunlock ${____filestruct__STDOUT_} 0.4
}





require(){
    ( command -v "${1}" >/dev/null ) || {
        sayline "${tab}!! skipping test, missing command ${1}"
        return 1
    }
}

inspect_cpuinfo(){
    readcmd "cat /proc/cpuinfo" ${____path__proc_cpuinfo_}
}


inspect_meminfo(){
    readcmd "cat /proc/meminfo" ${____path__proc_meminfo_}
}


inspect_driveinfo(){
    readcmd 'cat /proc/partitions' ${____path__proc_partitions_}

    require mount && readcmd 'mount -m' ${____path__etc_fstab_}

    require blkid && readcmd 'blkid' # no locks here: it handles itself

    require lsblk && readcmd 'lsblk' # no locks here: it handles itself
}
mount_mem(){
    flock    $$____path__etc_fstab_
    require mount && mount -a
    flunlock $$____path__etc_fstab_
}


readvectors(){
    readcmd 'cat data.ai' ${____path_HERE__file_1_}
}


checkterm(){
    (( $COLUMNS <= 88 )) \
        && printf "!! please resize your terminal, %s%s\n" \
                  "at least 96 columns are needed for    " \
                  "   taswelll       AI to work properly." \
        && exit 1
}





# if   taswelll   will be verbose
vvarg=`join '' $@`
vverb="${vvarg//[^v]}"
v="${#vverb}"

sayline "Initializing..."


sayline "[-${tab}inspecting cpu]"
v_cpuinfo=$(inspect_cpuinfo)
if (( $v >= 2 )); then
    saylines "${v_cpuinfo}"
fi


sayline "[-${tab}inspecting memory]"
v_meminfo=$(inspect_meminfo)
if (( $v >= 2 )); then
    saylines "${v_meminfo}"
fi


sayline "[-${tab}inspecting drive mounts]"
v_driveinfo=$(inspect_driveinfo)
if (( $v >= 2 )); then
    sayline "${v_driveinfo}"
fi


sayline "[-${tab}mounting memory]"
mount_mem


sayline "[-${tab}loading vectors]"
taswelll_ai_vectors=$(readvectors)
if (( $v >= 3 )); then
    saylines "${taswelll_ai_vectors}"
fi

checkterm

sayline "Done initializing."
saylines "" "" ""

sayline "${tab}${tab}${tab}${tab}Welcome  to    taswelll    AI"


readcmd 'cat taswelll_240x240.ansiclr'
echo -ne '\x1b[0m\n\n'


while read -e -p '(prompt) >>> ' -r prompt; do
    printf "%s\n\n\n" "${taswelll_ai_vectors}"
done
