#!/bin/bash
######################################################################
#Copyright (C) 2022  Kris Occhipinti
#https://filmsbykris.com

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
######################################################################

#This Script Makes the Thinkpad cover LED flash in morse code
led="/sys/class/leds/tpacpi::lid_logo_dot/brightness"

msg="$*"

[[ $msg ]] ||read -p "Enter Messages: " msg
[[ $msg ]] || exit 1

string="$(echo -n "$msg" |morsegen -)"
echo "$string"

echo "0" > "$led"
sleep 1

for (( i=0; i<${#string}; i++ ))
do
  echo -n "${string:$i:1}"
  
  [[ "${string:$i:1}" == "." ]] && t=.2
  [[ "${string:$i:1}" == "-" ]] && t=.4

  #if space turn off for 1 second
  if [[ "${string:$i:1}" == " " ]]
  then
    t=1
  else
    echo "1" > "$led"
    sleep $t
  fi
  echo "0" > "$led"
  sleep $t
done

sleep 1 
echo
echo "1" > "$led"
