# Bash functions to display OK/ERROR and play sounds after commands finish.
# Written for Ubuntu. Should work on any Linux after changing the paths.
# They have to be sourced manually or in ~/.bashrc or ~/.profile
# See: https://github.com/rsp/scripts/blob/master/ok-functions.md

sound() {
  # plays sounds in sequence and waits for them to finish
  for s in $@; do
    paplay $s
  done
}
soundbg() {
  # plays all sounds at the same time in the background
  for s in $@; do
    # you may need to change 0 to 1 or something else:
    pacmd play-file $s 0 >/dev/null
  done
}
soundloop() {
  # soundloop file
  # plays a sound in a loop
  set +m
  a=`date +%s`
  { paplay $1 & } 2>/dev/null
  wait
  b=`date +%s`
  d=$(($b-$a))
  [ $d -eq 0 ] && d=1
  while :; do
    pacmd play-file $1 0 >/dev/null
    sleep $d
  done
}
soundloopm() {
  # soundloop file n
  # plays a sound in a loop in intervals less than sound length by n seconds
  set +m
  a=`date +%s`
  { paplay $1 & } 2>/dev/null
  wait
  b=`date +%s`
  d=$(($b-$a-$n))
  [ $d -eq 0 ] && d=1
  while :; do
    pacmd play-file $1 0 >/dev/null
    sleep $d
  done
}
soundloops() {
  # soundloops file n
  # plays a sound in a loop every n seconds
  while :; do
    soundbg $1
    sleep $2
  done
}
sn1() {
  sound /usr/share/sounds/ubuntu/stereo/dialog-information.ogg
}
sn2() {
  sound /usr/share/sounds/freedesktop/stereo/complete.oga
}
sn3() {
  sound /usr/share/sounds/freedesktop/stereo/suspend-error.oga
}
loop1() {
  soundloop /usr/share/sounds/ubuntu/stereo/desktop-login.ogg
}
ok() {
  # prints OK or ERROR and exit status of previous command
  s=$?
  if [[ $s = 0 ]]; then
    echo OK
  else
    echo ERROR: $s
  fi
}
oks() {
  # like ok but with sounds
  s=$?
  sound_ok=/usr/share/sounds/ubuntu/stereo/dialog-information.ogg
  sound_error=/usr/share/sounds/ubuntu/stereo/dialog-warning.ogg
  if [[ $s = 0 ]]; then
    echo OK
    sound $sound_ok
  else
    echo ERROR: $s
    sound $sound_error
  fi
}