package main

import (
	"flag"
	"fmt"
	"io"
	"net/http"
	"os"
	"path"
)

func main() {
	flag.Parse()

	for _, arg := range flag.Args() {
		resp, err := http.Get(arg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: getting %s. Failed with: %s\n", arg, err)
			continue
		}

		if resp.StatusCode != 200 {
			fmt.Fprintf(os.Stderr, "%s response not ok\n", arg)
			continue
		}

		basename := path.Base(resp.Request.URL.Path)
		file, err := os.Create(basename)
		if err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: writing %s. Failed with: %s\n", basename, err)
			continue
		}
		io.Copy(file,resp.Body)
		file.Close()

		fmt.Println(basename)

	}
}
