:macro pw
;;This macro provides the authorization to known station types
;;In station database the TYPE and PWD fields must be set
;;When using some older version of FlexNet that use the magic numbers
;;algorithm just don't set the PWD field and set the SYSNUM field
if %(exist STN_TYPE) == 1

  set FOUND 0

  ;;Older FlexNet nodes use 'magic' numbers for authorization
  if %(exist STN_SYSNUM) == 1

    ;;Old Flexnet password
    if "%STN_TYPE" == "FLEXNET"
      flexpwd "%STN_SYSNUM"
      echo sys
      set FOUND 1
    endif

  else
  ;;Other algorithms use the password string
  if %(exist STN_PWD) == 1

    ;;New Flexnet password
    if "%STN_TYPE" == "FLEXNET"
      goto TNSTART ;; the same as TheNet password
    endif

    ;;FBB MD2 password
    if "%STN_TYPE" == "FBB"
      set PROMPT "%<"     ;;clear the input line buffer
      set PROMPT ""
      echo sys            ;;send the request
      flush
      :FBBLOOP            ;;wait for some prompt
      set PROMPT "%<"
      if ("%PROMPT" == "") goto FBBLOOP
      if "%STN_MD" == "5"
        md5pwd "%STN_PWD" "%PROMPT" ;;calculate the password
      else
        md2pwd "%STN_PWD" "%PROMPT" ;;calculate the password
      endif
      set FOUND 1
      unset PROMPT
    endif

    ;;TheNet password
    if "%STN_TYPE" == "THENET"
      :TNSTART
      set PROMPT "%<"     ;;clear the input line buffer
      set PROMPT ""
      echo sys            ;;send the request
      flush
      :TNLOOP             ;;wait for some prompt
      set PROMPT "%<"
      if ("%PROMPT" == "") goto TNLOOP
      tnpwd "%STN_PWD" "%PROMPT" ;;calculate the password
      :TNLOOP1
      if (%(exist TNPWD) == 0) goto TNLOOP1
      if (%TNPWD == 0) goto TNSTART
      unset TNPWD
      set FOUND 1
      unset PROMPT
    endif
    
  else
    echo No password defined for %C
  endif
  endif

  ;;Not found
  if %FOUND != 1
    echo Unknown station type %STN_TYPE
  endif
  unset FOUND

else
  echo Station type not defined for %C
endif
      