123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- #include "cms_util.h"
- #include "cms_err.h"
- #include "cms_files.h"
- #include "cms_page.h"
- #include "cms_build.h"
-
- int main(int argc, char** argv)
- {
- if (argc < 2)
- {
- fprintf(stderr, "Usage: %s <init|build>\n", argv[0]);
- return 1;
- }
-
- //Initiate
- if (strcmp(argv[1], "init") == 0)
- {
- if (argc != 3)
- {
- fprintf(stderr, "Usage: %s init <directory>\n", argv[0]);
- return 1;
- }
-
- char* dirname = argv[2];
-
- //Get the path of .cmsinited, which tells us
- //whether or not the directory is already inited
- char* initedPath = cms_util_path_join(dirname, CMS_FILE_INITED);
- if (initedPath == NULL)
- cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
-
- //Panic if the directory is already initiated
- if (cms_util_file_exists(initedPath))
- cms_err_panic(cms_err_create(CMS_ERR_INITED, NULL));
-
- //Copy files from resources
- cms_err* err;
- err = cms_util_dir_copy_recursive(CMS_FILE_RESOURCES, dirname);
- if (err)
- cms_err_panic(err);
-
- //Create .cmsinited file
- err = cms_util_file_create(initedPath);
- if (err)
- cms_err_panic(err);
- }
-
- //Build
- else if (strcmp(argv[1], "build") == 0)
- {
- if (argc != 3)
- {
- fprintf(stderr, "Usage: %s build <directory>\n", argv[0]);
- return 1;
- }
-
- char* dirname = argv[2];
-
- //Get the path of .cmsinited, which tells us
- //whether or not the directory is already inited
- char* initedPath = cms_util_path_join(dirname, CMS_FILE_INITED);
-
- //Panic if the directory isn't initiated
- if (!cms_util_file_exists(initedPath))
- cms_err_panic(cms_err_create(CMS_ERR_NOTINITED, NULL));
-
- cms_page* root = cms_page_create();
- if (root == NULL)
- cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
- root->slug = "";
- root->title = "";
-
- char* path = cms_util_path_join(dirname, CMS_FILE_ROOT);
- if (path == NULL)
- cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
-
- //Build tree of pages and posts
- cms_err* err;
- err = cms_build_make_tree(root, path, NULL);
- free(path);
- if (err)
- cms_err_panic(err);
-
- char* outpath = cms_util_path_join(dirname, CMS_FILE_OUT);
- if (outpath == NULL)
- cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
-
- err = cms_build_write_files(root, outpath, dirname);
- if (err)
- cms_err_panic(err);
- }
-
- //Nothing, print usage
- else
- {
- fprintf(stderr, "Usage: %s <init|build>\n", argv[0]);
- return 1;
- }
- }
|