           menu: true,
           menu_text: "Home",
           menu_icon: ""}
-      ], pretty: true, indent: 2
-      IO.write f, tmp
-    end
+      ], pretty: true, indent: 2)
+      IO.write(f, tmp)
+    end)
     IO.puts "Generated `#{dir}pages/pages.json`."
+    dir
   end
 
-  defp init(:templates, dir) do
+  defp init(dir, :templates) do
     %{base: template_base,
       nav:  template_nav,
       list: template_list,

        date_format: "{WDFull}, {D} {Mshort} {YYYY}",
        preview_length: 200}
      |> Poison.encode!(pretty: true, indent: 2)
    File.open! "#{dir}serum.json", [:write, :utf8], fn f ->
      IO.write f, projinfo
    end
    IO.puts "Generated `#{dir}serum.json`."
  end

  defp init(:page, dir) do
    File.open! "#{dir}pages/index.md", [:write, :utf8], fn f ->
      IO.write f, "*Hello, world!*\n"
    end
    File.open! "#{dir}pages/pages.json", [:write, :utf8], fn f ->
      tmp = Poison.encode! [
        %Serum.Pageinfo{
          name: "index",
          type: "md",


  def init(dir) do
    dir = if String.ends_with?(dir, "/"), do: dir, else: dir <> "/"

    dir |> check_dir
        |> init(:dir)
        |> init(:infofile)
        |> init(:page)
        |> init(:templates)
        |> init(:gitignore)
        |> finish
  end

  defp check_dir(dir) do
    if File.exists? dir do
      IO.puts "\x1b[93mWarning: The directory `#{dir}` " <>
              "already exists and might not be empty.\x1b[0m"
    end
    dir
  end

  defp finish(dir) do
    IO.puts "\n\x1b[1mSuccessfully initialized a new Serum project!"
    IO.puts "try `serum build #{dir}` to build the site.\x1b[0m\n"
  end

  defp init(dir, :dir) do
    ["posts", "pages", "media", "templates",
     "assets/css", "assets/js", "assets/images"]
    |> Enum.each(fn x ->
      File.mkdir_p!("#{dir}#{x}")
      IO.puts "Created directory `#{dir}#{x}`."
    end)
    dir
  end

  defp init(dir, :infofile) do
    projinfo =
      %{site_name: "New Website",
        site_description: "Welcome to my website!",
