+ fprintf(f, HTML_VERSION_TEMPLATE,
+ version_to_json(ppa, pkg, version));
+
+ footer = get_footer();
+ if (footer)
+ fputs(footer, f);
+
+ fclose(f);
+
+ free(path);
+ free(f_name);
+}
+
+static void
+create_html(const char *path,
+ const char *title,
+ const char *body_template,
+ const char *script)
+{
+ FILE *f;
+ const char *footer;
+
+ f = fopen(path, "w");
+
+ if (!f) {
+ log_err(_("failed to open: %s"), path);
+ return ;
+ }
+
+ fprintf(f, HTML_HEADER, title, script);
+ fputs(body_template, f);
+
+ footer = get_footer();
+ if (footer)
+ fputs(footer, f);
+
+ fclose(f);
+}
+
+static char *ppa_display_name(const struct ppa_stats *ppa)
+{
+ char *ret;
+
+ ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
+
+ sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
+
+ return ret;
+}
+
+static void
+index_to_html(struct ppa_stats *ppa, const char *dir)
+{
+ char *path, *json_path, *dname;
+ json_object *json;
+ const char *body;
+
+ body = get_ppa_body();
+ if (!body) {
+ log_err("Failed to create PPA page");
+ return ;
+ }
+
+ json = ppa_to_json(ppa);
+ json_path = path_new(dir, "index", ".json");
+
+ log_debug(_("generating %s"), json_path);
+ json_object_to_file(json_path, json);
+ json_object_put(json);
+ free(json_path);
+
+ path = path_new(dir, "index", ".html");
+ dname = ppa_display_name(ppa);
+ create_html(path, dname, body, "ppastats_ppa();");
+ free(path);
+ free(dname);
+}
+
+static void
+pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
+{
+ char *path, *json_path, *script;
+ json_object *json;
+ const char *body;
+
+ body = get_pkg_body();
+ if (!body) {
+ log_err("Failed to create package page: %s", pkg->name);
+ return ;
+ }
+
+ json_path = path_new(dir, pkg->name, ".json");
+ json = pkg_to_json(ppa, pkg);
+ log_debug(_("generating %s"), json_path);
+
+ json_object_to_file(json_path, json);
+ json_object_put(json);
+ free(json_path);
+
+ path = path_new(dir, pkg->name, ".html");
+ script = malloc(strlen("ppastats_pkg(\"\");")+
+ strlen(pkg->name)+
+ strlen(".json")+
+ 1);
+ sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
+
+ log_debug(_("generating %s"), path);
+
+ create_html(path, pkg->name, body, script);
+ free(path);
+ free(script);
+}
+
+static void
+pkgs_to_html(struct ppa_stats *ppa,
+ struct package_stats **pkgs,
+ const char *dir)
+{
+ struct version_stats **versions;
+
+ while (*pkgs) {
+ pkg_to_html(ppa, *pkgs, dir);
+
+ versions = (*pkgs)->versions;
+ while (*versions) {
+ version_to_html(ppa, *pkgs, *versions, dir);
+
+ versions++;
+ }
+
+ pkgs++;
+ }
+}
+
+void
+ppa_to_html(const char *owner,
+ const char *ppa,
+ const char *package_status,
+ const char *output_dir,
+ const int install_static_files)
+{
+ struct ppa_stats *ppastats;
+ char *path, *f_dst;
+ char *css_dir, *js_dir;
+ int i;
+ static char *www_files[]
+ = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
+ DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
+ DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
+ "js/jqplot.dateAxisRenderer.min.js",
+ DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
+ "js/jquery.jqplot.min.js",
+ DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
+ DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
+ DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
+ "css/jquery.jqplot.min.css" };
+
+ mkdirs(output_dir, 0777);
+
+ if (install_static_files) {
+ css_dir = path_append(output_dir, "css");
+ js_dir = path_append(output_dir, "js");
+
+ mkdir(css_dir, 0777);
+ mkdir(js_dir, 0777);
+
+ for (i = 0; i < 7; i++) {
+ f_dst = path_append(output_dir, www_files[2*i+1]);
+
+ log_debug(_("copying %s %s"), www_files[2*i], f_dst);
+ fcopy(www_files[2*i], f_dst);
+
+ free(f_dst);
+ }
+ free(css_dir);
+ free(js_dir);
+ }
+
+ ppastats = create_ppa_stats(owner, ppa, package_status);
+
+ path = path_new(output_dir, "ppa", ".html");
+
+ pkgs_to_html(ppastats, ppastats->packages, output_dir);
+
+ index_to_html(ppastats, output_dir);