processCmd :: Cmd -> IO ()
processCmd Init = do
  putStrLn "Ghost: server-side initialization."
  home <- getHomeDirectory
  let authorizedKeys = home </> ".ssh" </> "authorized_keys"
      administratorKeys = home </> "administrator" </> "keys"

  _ <- createDirectoryIfMissing True $ home </> "administrator"
  _ <- createDirectoryIfMissing True $ home </> "users"
  _ <- createDirectoryIfMissing True $ home </> "gits"
  _ <- createDirectoryIfMissing True $ home </> "run/staging/nginx"
  _ <- createDirectoryIfMissing True $ home </> "run/production/nginx"
  withFile (home </> "run" </> "nginx.conf") WriteMode $ \h -> do
    hPutStrLn h $ "include " ++ home ++ "/run/staging/nginx/*.conf;"
    hPutStrLn h $ "include " ++ home ++ "/run/production/nginx/*.conf;"

  b <- doesFileExist $ authorizedKeys
  if b
    then do
      putStrLn $ "Prior authorized_keys file found, " ++
        "copying it as administrator key."
      -- TODO make sure there is a single key in it. For now, it assumes this
      -- is the key copied by setup-ghost-archlinux.sh.
      copyFile authorizedKeys administratorKeys
    else putStrLn $ "No prior authorized_keys file found."
  refreshAuthorizedKeys "/home/ghost/bin/ghost-command"

  putStrLn "Ghost server-side initialization complete."
  putStrLn "Add the following line to your main Nginx configuration"
  putStrLn "file (inside an `http {..}` block.):"
  putStrLn $ "  include " ++ home ++ "/run/nginx.conf;"
  putStrLn "Add the following line to your sudoers file:"
  putStrLn "  ghost   ALL=(ALL) NOPASSWD: /etc/rc.d/nginx"
  putStrLn "(so the post-update hook can instruct Nginx to reload its configuration)."
processCmd :: Cmd -> IO ()
processCmd Init = do
  putStrLn "Ghost: server-side initialization."
  home <- getHomeDirectory
  let authorizedKeys = home </> ".ssh" </> "authorized_keys"
      administratorKeys = home </> "administrator" </> "keys"

  _ <- createDirectoryIfMissing True $ home </> "administrator"
  _ <- createDirectoryIfMissing True $ home </> "users"
  _ <- createDirectoryIfMissing True $ home </> "gits"
  _ <- createDirectoryIfMissing True $ home </> "run/staging/nginx"
  _ <- createDirectoryIfMissing True $ home </> "run/production/nginx"
  withFile (home </> "run" </> "nginx.conf") WriteMode $ \h -> do
    hPutStrLn h $ "include " ++ home ++ "/run/staging/nginx/*.conf;"
    hPutStrLn h $ "include " ++ home ++ "/run/production/nginx/*.conf;"

  b <- doesFileExist $ authorizedKeys
  if b
    then do
      putStrLn $ "Prior authorized_keys file found, " ++
        "copying it as administrator key."
      -- TODO make sure there is a single key in it. For now, it assumes this
      -- is the key copied by setup-ghost-archlinux.sh.
      copyFile authorizedKeys administratorKeys
    else putStrLn $ "No prior authorized_keys file found."
  refreshAuthorizedKeys "/home/ghost/bin/ghost-command"

  putStrLn "Ghost server-side initialization complete."
  putStrLn "Add the following line to your main Nginx configuration"
  putStrLn "file (inside an `http {..}` block.):"
  putStrLn $ "  include " ++ home ++ "/run/nginx.conf;"
  putStrLn "Add the following line to your sudoers file:"
  putStrLn "  ghost   ALL=(ALL) NOPASSWD: /etc/rc.d/nginx"
  putStrLn "(so the post-update hook can instruct Nginx to reload its configuration)."
processCmd Init = do
  putStrLn "Ghost: server-side initialization."
  home <- getHomeDirectory
  let authorizedKeys = home </> ".ssh" </> "authorized_keys"
      administratorKeys = home </> "administrator" </> "keys"

  _ <- createDirectoryIfMissing True $ home </> "administrator"
  _ <- createDirectoryIfMissing True $ home </> "users"
  _ <- createDirectoryIfMissing True $ home </> "gits"
  _ <- createDirectoryIfMissing True $ home </> "run/staging/nginx"
  _ <- createDirectoryIfMissing True $ home </> "run/production/nginx"
  withFile (home </> "run" </> "nginx.conf") WriteMode $ \h -> do
    hPutStrLn h $ "include " ++ home ++ "/run/staging/nginx/*.conf;"
    hPutStrLn h $ "include " ++ home ++ "/run/production/nginx/*.conf;"

  b <- doesFileExist $ authorizedKeys
  if b
    then do
      putStrLn $ "Prior authorized_keys file found, " ++
        "copying it as administrator key."
      -- TODO make sure there is a single key in it. For now, it assumes this
      -- is the key copied by setup-ghost-archlinux.sh.
      copyFile authorizedKeys administratorKeys
    else putStrLn $ "No prior authorized_keys file found."
  refreshAuthorizedKeys "/home/ghost/bin/ghost-command"

  putStrLn "Ghost server-side initialization complete."
  putStrLn "Add the following line to your main Nginx configuration"
  putStrLn "file (inside an `http {..}` block.):"
  putStrLn $ "  include " ++ home ++ "/run/nginx.conf;"
  putStrLn "Add the following line to your sudoers file:"
  putStrLn "  ghost   ALL=(ALL) NOPASSWD: /etc/rc.d/nginx"
  putStrLn "(so the post-update hook can instruct Nginx to reload its configuration)."