c4e0846fe8d137faba83cbc345d2a9478775cab3
[reposerve] / server / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include "repo.h"
6
7 static const char *guess_repo_name(const char *path);
8 static int parse_args(int argc, char **argv);
9
10 static const char *repo_path, *repo_name;
11
12 int main(int argc, char **argv)
13 {
14         if(parse_args(argc, argv) == -1) {
15                 return 1;
16         }
17
18         if(repo_init(repo_path) == -1) {
19                 return 1;
20         }
21         /* repo_cleanup(); */
22
23         if(!repo_name) {
24                 if(!(repo_name = guess_repo_name(repo_path))) {
25                         return 1;
26                 }
27         }
28
29         printf("Serving %s from %s\n", repo_name, repo_path);
30
31         /* DBG */
32         {
33                 int i, count = repo_num_files();
34                 for(i=0; i<count; i++) {
35                         printf("%s\n", repo_file(i));
36                 }
37         }
38
39         return 0;
40 }
41
42 static const char *guess_repo_name(const char *path)
43 {
44         static char pathbuf[PATH_MAX];
45         char *last_slash;
46         int len;
47
48         if(!realpath(path, pathbuf)) {
49                 goto fail;
50         }
51         len = strlen(pathbuf);
52
53         if(len > 0 && pathbuf[len - 1] == '/') {
54                 pathbuf[--len] = 0;
55         }
56         if(len <= 0) goto fail;
57
58         if((last_slash = strrchr(pathbuf, '/'))) {
59                 return last_slash + 1;
60         }
61         return pathbuf;
62
63 fail:
64         fprintf(stderr, "failed to resolve path %s while determining the repo name. Please specify an explicit name with -id\n", repo_path);
65         return 0;
66 }
67
68 static void print_usage(const char *argv0)
69 {
70         printf("Usage: %s [options] [repo path]\n", argv0);
71         printf("options:\n");
72         printf("  -id <name>: repo identifier (defaults to directory name)\n");
73         printf("  -h,-help: print help and exit\n");
74 }
75
76 static int parse_args(int argc, char **argv)
77 {
78         int i;
79
80         for(i=1; i<argc; i++) {
81                 if(argv[i][0] == '-') {
82                         if(strcmp(argv[i], "-id") == 0) {
83                                 repo_name = argv[++i];
84                         } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
85                                 print_usage(argv[0]);
86                                 exit(0);
87                         } else {
88                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
89                                 return -1;
90                         }
91                 } else {
92                         if(repo_path) {
93                                 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
94                                 return -1;
95                         }
96                         repo_path = argv[i];
97                 }
98         }
99
100         if(!repo_path) repo_path = ".";
101         return 0;
102 }