procItem lock gasupply c item = procIfRobotsOK lock gasupply c item $
    do msg $ show item          -- Show what we're up to
       let fspath = getFSPath item

       -- Create the directory for the file to go in, if necessary.
       catch (bracket_ (acquire lock) 
                       (release lock) 
                       (createDirectoryIfMissing True 
                        (fst . splitFileName $ fspath)))
             (\e -> -- If we got an exception here, note an error for this item
                    do msg $ "Single-Item Error on " ++ (show item) ++ ": " 
                           ++ (show e)
                       updateItem lock c item ErrorState (show e)
             )

       fh <- catch ((openFile fspath WriteMode >>= (return . Just)))
             (\e -> do msg $ "Single-item error on " ++ (show item) ++ ": " ++
                           (show e)
                       updateItem lock c item ErrorState (show e)
                       return Nothing
             )

       case fh of
         Nothing -> return ()
         Just h -> -- Now, download it.  If it's a menu
                   --(item type 1), check it for links
                   -- (spider it).  Error here means a TCP
                   -- problem, so mark every
                   -- item on this host as having the error.
                   catch (do dlItem item h
                             when (dtype item == '1') (spider lock c fspath)
                             updateItem lock c item Visited ""
                         )
                         (\e -> do msg $ "Error on " ++ (show item)
                                           ++ ": " ++ (show e)
                                   noteErrorOnHost lock gasupply c 
                                                   (host item) (show e)
                         )
                  
{- | This function is called by procItem whenever it downloads a
menu.  This function calles the parser, extracts items, and calles
DB.queueItems to handle them.  (Insert into DB if new) -}
procItem lock gasupply c item = procIfRobotsOK lock gasupply c item $
    do msg $ show item          -- Show what we're up to
       let fspath = getFSPath item

       -- Create the directory for the file to go in, if necessary.
       catch (bracket_ (acquire lock) 
                       (release lock) 
                       (createDirectoryIfMissing True 
                        (fst . splitFileName $ fspath)))
             (\e -> -- If we got an exception here, note an error for this item
                    do msg $ "Single-Item Error on " ++ (show item) ++ ": " 
                           ++ (show e)
                       updateItem lock c item ErrorState (show e)
             )

       fh <- catch ((openFile fspath WriteMode >>= (return . Just)))
             (\e -> do msg $ "Single-item error on " ++ (show item) ++ ": " ++
                           (show e)
                       updateItem lock c item ErrorState (show e)
                       return Nothing
             )

       case fh of
         Nothing -> return ()
         Just h -> -- Now, download it.  If it's a menu
                   --(item type 1), check it for links
                   -- (spider it).  Error here means a TCP
                   -- problem, so mark every
                   -- item on this host as having the error.
                   catch (do dlItem item h
                             when (dtype item == '1') (spider lock c fspath)
                             updateItem lock c item Visited ""
                         )
                         (\e -> do msg $ "Error on " ++ (show item)
                                           ++ ": " ++ (show e)
                                   noteErrorOnHost lock gasupply c 
                                                   (host item) (show e)
                         )
                  
{- | This function is called by procItem whenever it downloads a
menu.  This function calles the parser, extracts items, and calles
DB.queueItems to handle them.  (Insert into DB if new) -}
procItem lock gasupply c item = procIfRobotsOK lock gasupply c item $
    do msg $ show item          -- Show what we're up to
       let fspath = getFSPath item

       -- Create the directory for the file to go in, if necessary.
       catch (bracket_ (acquire lock) 
                       (release lock) 
                       (createDirectoryIfMissing True 
                        (fst . splitFileName $ fspath)))
             (\e -> -- If we got an exception here, note an error for this item
                    do msg $ "Single-Item Error on " ++ (show item) ++ ": " 
                           ++ (show e)
                       updateItem lock c item ErrorState (show e)
             )

       fh <- catch ((openFile fspath WriteMode >>= (return . Just)))
             (\e -> do msg $ "Single-item error on " ++ (show item) ++ ": " ++
                           (show e)
                       updateItem lock c item ErrorState (show e)
                       return Nothing
             )

       case fh of
         Nothing -> return ()
         Just h -> -- Now, download it.  If it's a menu
                   --(item type 1), check it for links
                   -- (spider it).  Error here means a TCP
                   -- problem, so mark every
                   -- item on this host as having the error.
                   catch (do dlItem item h
                             when (dtype item == '1') (spider lock c fspath)
                             updateItem lock c item Visited ""
                         )
                         (\e -> do msg $ "Error on " ++ (show item)
                                           ++ ": " ++ (show e)
                                   noteErrorOnHost lock gasupply c 
                                                   (host item) (show e)
                         )
                  
{- | This function is called by procItem whenever it downloads a
menu.  This function calles the parser, extracts items, and calles
DB.queueItems to handle them.  (Insert into DB if new) -}