import Data.List.Split (splitOn)
import System.Process
import System.IO
import System.FilePath
import System.IO.Strict



main = fullmake "." "1"

writeFile :: FilePath -> String -> IO ()
writeFile f s
  = do h <- openFile f WriteMode
       hSetEncoding h utf8
       hPutStr h s
       hClose h

readFile :: FilePath -> IO String
readFile f
  = do h <- openFile f ReadMode
       hSetEncoding h utf8
       z <- System.IO.Strict.hGetContents h
       return z



fullmake ::String->String -> IO ()
fullmake dir s = do _ <- system ("curl --retry 3 -s -w \"%{http_code} %{url} %{filename_effective}\\n\" --compressed -K "++(dir </> ("curlrun"++s))++" --parallel -L > "++(dir </> ("curloutput"++s++".1")))
                    mymake dir s 1 

mymake :: String->String->Integer-> IO ()
mymake dir s n = do text <-Main.readFile (dir </>("curloutput"++s++"."++(show n)))
                    let list=(filter pred (map (splitOn " ") (splitOn "\n" text)))
                    putStrLn ("Number of Failures "++(show (length list)))
                    Main.writeFile (dir </>("curlrun"++s++"."++(show(n+1)))) (concat ((map jjoin list)::[String]))
                    system ("curl --retry 3 -s --compressed -K "++(dir</> ("curlrun"++s++"."++(show (n+1))))++" --parallel -L -w \"%{http_code} %{url} %{filename_effective}\\n\"  > "++(dir </> ("curloutput"++s++"."++(show (n+1)))))
                    if ((list==[])) then return () else (mymake dir s (n+1))
  where 
    pred (x:xs) | x=="429" = True
    pred _ = False
    jjoin:: [String]->String
    jjoin (x:y:z:[]) = "url = \""++y++"\"\noutput = \""++z++"\"\n"
