getVersion :: Verbosity -> FilePath -> IO (Maybe Version)
getVersion verbosity hugsPath = do
  (output, _err, exit) <- rawSystemStdInOut verbosity hugsPath []
                              (Just (":quit", False)) False
  if exit == ExitSuccess
    then return $! findVersion output
    else return Nothing

  where
    findVersion output = do
      (monthStr, yearStr) <- selectWords output
      year  <- convertYear yearStr
      month <- convertMonth monthStr
      return (Version [year, month] [])

    selectWords output =
      case [ (month, year)
           | [_,_,"Version:", month, year,_] <- map words (lines output) ] of
        [(month, year)] -> Just (month, year)
        _               -> Nothing
    convertYear year = case reads year of
      [(y, [])] | y >= 1999 && y < 2020 -> Just y
      _                                 -> Nothing
    convertMonth month = lookup month (zip months [1..])
    months = [ "January", "February", "March", "April", "May", "June", "July"
             , "August", "September", "October", "November", "December" ]
getVersion :: Verbosity -> FilePath -> IO (Maybe Version)
getVersion verbosity hugsPath = do
  (output, _err, exit) <- rawSystemStdInOut verbosity hugsPath []
                              (Just (":quit", False)) False
  if exit == ExitSuccess
    then return $! findVersion output
    else return Nothing

  where
    findVersion output = do
      (monthStr, yearStr) <- selectWords output
      year  <- convertYear yearStr
      month <- convertMonth monthStr
      return (Version [year, month] [])

    selectWords output =
      case [ (month, year)
           | [_,_,"Version:", month, year,_] <- map words (lines output) ] of
        [(month, year)] -> Just (month, year)
        _               -> Nothing
    convertYear year = case reads year of
      [(y, [])] | y >= 1999 && y < 2020 -> Just y
      _                                 -> Nothing
    convertMonth month = lookup month (zip months [1..])
    months = [ "January", "February", "March", "April", "May", "June", "July"
             , "August", "September", "October", "November", "December" ]
getVersion verbosity hugsPath = do
  (output, _err, exit) <- rawSystemStdInOut verbosity hugsPath []
                              (Just (":quit", False)) False
  if exit == ExitSuccess
    then return $! findVersion output
    else return Nothing

  where
    findVersion output = do
      (monthStr, yearStr) <- selectWords output
      year  <- convertYear yearStr
      month <- convertMonth monthStr
      return (Version [year, month] [])

    selectWords output =
      case [ (month, year)
           | [_,_,"Version:", month, year,_] <- map words (lines output) ] of
        [(month, year)] -> Just (month, year)
        _               -> Nothing
    convertYear year = case reads year of
      [(y, [])] | y >= 1999 && y < 2020 -> Just y
      _                                 -> Nothing
    convertMonth month = lookup month (zip months [1..])
    months = [ "January", "February", "March", "April", "May", "June", "July"
             , "August", "September", "October", "November", "December" ]