!!******************************************************************************
!!
!!  This file is part of the AMUN source code, a program to perform
!!  Newtonian or relativistic magnetohydrodynamical simulations on uniform or
!!  adaptive mesh.
!!
!!  Copyright (C) 2019 Grzegorz Kowal <grzegorz@amuncode.org>
!!
!!  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/>.
!!
!!******************************************************************************
!!
!! module: HELPERS
!!
!!  This module provides miscellaneous support subroutines.
!!
!!
!!******************************************************************************
!
module helpers

! module variables are not implicit by default
!
  implicit none

! MODULE INTERFACES:
! =================
!
  interface print_parameter
    module procedure print_parameter_integer
    module procedure print_parameter_double
    module procedure print_parameter_string
  end interface

! by default everything is private
!
  private

! declare public subroutines
!
  public :: print_welcome, print_section, print_parameter

!- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!
  contains
!
!===============================================================================
!!
!!***  PUBLIC SUBROUTINES  *****************************************************
!!
!===============================================================================
!
!===============================================================================
!
! subroutine PRINT_WELCOME:
! ------------------------
!
!   Subroutine prints welcome message.
!
!   Arguments:
!
!     verbose - if true, the subroutine is executed, otherwise it is skipped;
!
!===============================================================================
!
  subroutine print_welcome(verbose)

! local variables are not implicit by default
!
    implicit none

! subroutine arguments
!
    logical, intent(in) :: verbose
!
!-------------------------------------------------------------------------------
!
    if (.not. verbose) return

    write(*,"(1x,78('-'))")
    write(*,"(1x,18('='),17x,a,17x,19('='))") 'A M U N'
    write(*,"(1x,16('='),4x,a,4x,16('='))")                                    &
                                      'Copyright (C) 2008-2019 Grzegorz Kowal'
    write(*,"(1x,18('='),9x,a,9x,19('='))")                                    &
                                        'under GNU GPLv3 license'
    write(*,"(1x,78('-'))")

!-------------------------------------------------------------------------------
!
  end subroutine print_welcome
!
!===============================================================================
!
! subroutine PRINT_SECTION:
! ------------------------
!
!   Subroutine prints section lines.
!
!   Arguments:
!
!     verbose - if true, the subroutine is executed, otherwise it is skipped;
!     title   - the section title;
!
!===============================================================================
!
  subroutine print_section(verbose, title)

! local variables are not implicit by default
!
    implicit none

! subroutine arguments
!
    logical         , intent(in) :: verbose
    character(len=*), intent(in) :: title
!
!-------------------------------------------------------------------------------
!
    if (.not. verbose) return

    write(*,*)
    write(*,"(1x,a,':')") trim(adjustl(title))

!-------------------------------------------------------------------------------
!
  end subroutine print_section
!
!===============================================================================
!
! subroutine PRINT_PARAMETER_INTEGER:
! ----------------------------------
!
!   Subroutine prints integer parameter.
!
!   Arguments:
!
!     verbose    - if true, the subroutine is executed, otherwise it is skipped;
!     desciption - the parameter description;
!     value      - the parameter value;
!
!===============================================================================
!
  subroutine print_parameter_integer(verbose, description, value)

! local variables are not implicit by default
!
    implicit none

! subroutine arguments
!
    logical         , intent(in) :: verbose
    character(len=*), intent(in) :: description
    integer         , intent(in) :: value

! local variables
!
    character(len=26) :: msg
!
!-------------------------------------------------------------------------------
!
    if (.not. verbose) return

    msg = trim(adjustl(description))
    write(*,"(4x,a26,1x,'=',1x,i0)") msg, value

!-------------------------------------------------------------------------------
!
  end subroutine print_parameter_integer
!
!===============================================================================
!
! subroutine PRINT_PARAMETER_DOUBLE:
! ----------------------------------
!
!   Subroutine prints double precision parameter.
!
!   Arguments:
!
!     verbose    - if true, the subroutine is executed, otherwise it is skipped;
!     desciption - the parameter description;
!     value      - the parameter value;
!
!===============================================================================
!
  subroutine print_parameter_double(verbose, description, value)

! local variables are not implicit by default
!
    implicit none

! subroutine arguments
!
    logical         , intent(in) :: verbose
    character(len=*), intent(in) :: description
    real(kind=8)    , intent(in) :: value

! local variables
!
    character(len=26) :: msg
!
!-------------------------------------------------------------------------------
!
    if (.not. verbose) return

    msg = trim(adjustl(description))
    if (value >= 0.0d+00) then
      write(*,"(4x,a26,1x,'=',es9.2)") msg, value
    else
      write(*,"(4x,a26,1x,'=',1x,es9.2)") msg, value
    end if

!-------------------------------------------------------------------------------
!
  end subroutine print_parameter_double
!
!===============================================================================
!
! subroutine PRINT_PARAMETER_STRING:
! ---------------------------------
!
!   Subroutine prints string parameter.
!
!   Arguments:
!
!     verbose    - if true, the subroutine is executed, otherwise it is skipped;
!     desciption - the parameter description;
!     value      - the parameter value;
!
!===============================================================================
!
  subroutine print_parameter_string(verbose, description, value)

! local variables are not implicit by default
!
    implicit none

! subroutine arguments
!
    logical         , intent(in) :: verbose
    character(len=*), intent(in) :: description
    character(len=*), intent(in) :: value

! local variables
!
    character(len=26) :: msg
!
!-------------------------------------------------------------------------------
!
    if (.not. verbose) return

    msg = trim(adjustl(description))
    write(*,"(4x,a26,1x,'=',1x,a)") msg, trim(adjustl(value))

!-------------------------------------------------------------------------------
!
  end subroutine print_parameter_string

!===============================================================================
!
end module helpers