! SPDX-License-Identifier: AGPL-3.0-or-later
! Copyright 2024-2025 Max-Planck-Institut für Nachhaltige Materialien GmbH
! Copyright 2011-2024 Max-Planck-Institut für Eisenforschung GmbH
!
! DAMASK is free software: you can redistribute it and/or modify
! it under the terms of the GNU Affero 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 Affero General Public License for more details.
!
! You should have received a copy of the GNU Affero General Public License
! along with this program.  If not, see <https://www.gnu.org/licenses/>.
!--------------------------------------------------------------------------------------------------
!> @author Martin Diehl, Max-Planck-Institut für Eisenforschung GmbH
!> @brief Handling of UNIX signals.
!--------------------------------------------------------------------------------------------------
module signal
  use prec
  use iso_c_binding

  implicit none(type,external)
  private

  logical(C_BOOL), public, protected, volatile, bind(C, name='f_sigint') :: &
    signal_SIGINT  = .false._C_BOOL
  logical(C_BOOL), public, protected, volatile, bind(C, name='f_sigusr1') :: &
    signal_SIGUSR1 = .false._C_BOOL
  logical(C_BOOL), public, protected, volatile, bind(C, name='f_sigusr2') :: &
    signal_SIGUSR2 = .false._C_BOOL
  public :: &
    signal_init, &
    signal_setSIGINT, &
    signal_setSIGUSR1, &
    signal_setSIGUSR2

  interface

    subroutine init_signal_C() bind(C)
    end subroutine init_signal_C

  end interface

contains


!--------------------------------------------------------------------------------------------------
!> @brief Init C signal handler.
!--------------------------------------------------------------------------------------------------
subroutine signal_init()

  call init_signal_C()

end subroutine signal_init


!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signal_SIGINT.
!--------------------------------------------------------------------------------------------------
subroutine signal_setSIGINT(state)

  logical, intent(in) :: state


  signal_SIGINT = logical(state,C_BOOL)
  print '(/a)', 'SIGINT ' // trim(merge('received', 'cleared ', state))

end subroutine signal_setSIGINT

!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signal_SIGUSR.
!--------------------------------------------------------------------------------------------------
subroutine signal_setSIGUSR1(state)

  logical, intent(in) :: state


  signal_SIGUSR1 = logical(state,C_BOOL)
  print '(/a)', 'SIGUSR1 ' // trim(merge('received', 'cleared ', state))

end subroutine signal_setSIGUSR1

!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signal_SIGUSR2.
!--------------------------------------------------------------------------------------------------
subroutine signal_setSIGUSR2(state)

  logical, intent(in) :: state


  signal_SIGUSR2 = logical(state,C_BOOL)
  print '(/a)', 'SIGUSR2 ' // trim(merge('received', 'cleared ', state))

end subroutine signal_setSIGUSR2

end module signal
