#!/usr/bin/env bash

# flow
# REPEATS via cmd[]. DESIGNED FOR HYPRLOCK SCRIPTING.
# check if file exists. If not, create.
# read the files
# assign variables to read files
# if letter is less than the word letters count, increment by 1, append the letter, and echo the result.
# other than that, proceed to blinking stage.

text="Gamer"
state_file="${XDG_RUNTIME_DIR:-/tmp}/typewriter_pos"
blink_file="${XDG_RUNTIME_DIR:-/tmp}/typewriter_blink"

if [[ ! -f $state_file ]]; then
  echo 0 >"$state_file"
fi

if [[ ! -f $blink_file ]]; then
  echo 0 >"$blink_file"
fi

len=${#text}
letter=$(cat "$state_file")
blink=$(cat "$blink_file")

if [[ $letter -lt $len ]]; then
  letter=$((letter + 1))
  echo "$letter" >"$state_file"
  echo "${text:0:$letter}_"
  sleep 0.2
else
  if ((blink % 2 == 0)); then
    echo "${text}_"
  else
    echo "${text} "
  fi
  sleep 1.1
fi

blink=$((blink + 1))
echo "$blink" >"$blink_file"
