#!/bin/bash

ctrlc_count=0


set -e -u

function no_ctrlc()
{
    let ctrlc_count++
    echo
    if [[ $ctrlc_count == 1 ]]; then
        echo "Stop that."
    elif [[ $ctrlc_count == 2 ]]; then
        echo "Once more and I quit."
    else
        echo "That's it.  I quit."
        exit
    fi
}

function helloWorld()
{
    echo "Hello Signal"
    ls
    ls
    echo $PWD
    # exit 0
}

function errorHandler()
{
    echo "error"
    exit 0
}


# trap no_ctrlc SIGINT
# trap helloWorld SIGUSR1
# trap helloWorld SIGUSR2
# trap errorHandler ERR


# Our USR1 signal handler
function usr1_trap(){
    printf 'Here is the PID: %d\nExiting right-now!\n' $$
    # exit
}

# Register USR1 signal handler
trap usr1_trap USR1


echo "PID: $BASHPID, $$"
printf 'Run this to stop me:\nkill -USR1 %d\n' $$


echo Sleeping

while true
do
    sleep .5
done



##printf 'Run this to stop me:\nkill -USR1 %d\n' $$
##
## Wait in background, not consuming CPU
#while :; do
#   sleep 9223372036854775807 & # int max (2^63 - 1)
#   wait $!
#done


echo End


#---fin--------------------------------------