module Main where

import System
import qualified System.IO as IO

--------------------------------------------------------------------------------
-- main loop
--
-- Endlessly generate the integer list [1..M], where M is read from the command
-- line, from back to front as a sequence of thunks.
---
main = do IO.hSetBuffering IO.stdout IO.LineBuffering
	  args <- getArgs :: IO [String]
	  if length args /= 1 then do prog <- getProgName
				      fail $ "command line: " ++ prog ++ " depth"
				      else return ()
	  depth <- let arg=args!!0 in catch (read arg) $ \_ -> fail $ "depth should be an integer not " ++ show arg :: IO Int
	  forever 0 depth
    where read :: (Read a) => String -> IO a
	  read xs = case [ x | (x,xs') <- reads xs, ("","") <- lex xs' ] of
		    [x] -> return x
		    _   -> fail "read: ambiguous parse"
	  forever :: Int -> Int -> IO ()
	  forever i d = do putStrLn $ "Starting block " ++ show i
			   build d [] `seq` forever (i+1) d
	  build :: Int -> [Int] -> [Int]
	  build 0 xs = xs
	  build d xs = build (d-1) $ d : xs
