-
Notifications
You must be signed in to change notification settings - Fork 4
/
smugup.c
189 lines (171 loc) · 4.56 KB
/
smugup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (C) 2008 Greg Kroah-Hartman <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include "list.h"
#include "md5.h"
#include "smugbatch_version.h"
#include "smug.h"
int debug;
static void display_help(void)
{
fprintf(stdout, "smugup - upload photos to smugmug.com\n");
fprintf(stdout, "Version: " SMUGBATCH_VERSION "\n");
fprintf(stdout, "Usage:\n");
fprintf(stdout, " smugup [options] files\n");
fprintf(stdout, "options are:\n");
fprintf(stdout, " --email email@address\n");
fprintf(stdout, " --password password\n");
fprintf(stdout, " --album album [ --category category"
" --settings quicksettings ]\n");
fprintf(stdout, " --quiet\n");
fprintf(stdout, " --debug\n");
fprintf(stdout, " --help\n");
}
int main(int argc, char *argv[], char *envp[])
{
static const struct option options[] = {
{ "debug", 0, NULL, 'd' },
{ "email", 1, NULL, 'e' },
{ "password", 1, NULL, 'p' },
{ "album", 1, NULL, 'a' },
{ "category", 1, NULL, 'c' },
{ "settings", 1, NULL, 's' },
{ "help", 0, NULL, 'h' },
{ "quiet", 0, NULL, 'q' },
{ }
};
struct filename *filename;
struct session *session;
struct album *album;
char *album_title = NULL;
char *category_title = NULL;
char *quicksettings_name = NULL;
int retval;
int option;
int i;
session = session_alloc();
if (!session) {
fprintf(stderr, "no more memory...\n");
return -1;
}
curl_global_init(CURL_GLOBAL_ALL);
smug_parse_configfile(session);
while (1) {
option = getopt_long_only(argc, argv, "dqe:p:a:c:s:h",
options, NULL);
if (option == -1)
break;
switch (option) {
case 'd':
debug = 1;
break;
case 'e':
if (session->email)
free(session->email);
session->email = strdup(optarg);
dbg("email = %s\n", session->email);
break;
case 'p':
if (session->password)
free(session->password);
session->password = strdup(optarg);
dbg("password = %s\n", session->password);
break;
case 'a':
album_title = strdup(optarg);
dbg("album_title = %s\n", album_title);
break;
case 'c':
category_title = strdup(optarg);
dbg("category_title = %s\n", category_title);
break;
case 's':
quicksettings_name = strdup(optarg);
dbg("quicksettings_name = %s\n", quicksettings_name);
break;
case 'q':
session->quiet = 1;
break;
case 'h':
display_help();
goto exit;
default:
display_help();
goto exit;
}
}
if ((category_title || quicksettings_name) && !album_title) {
fprintf(stdout, "Category and Settings requires Title.");
display_help();
goto exit;
}
if (!session->email) {
fprintf(stdout, "Enter smugmug.com email address: ");
session->email = get_string_from_stdin();
}
if (!session->password) {
fprintf(stdout, "Enter smugmug.com password: ");
session->password = get_string_from_stdin();
}
/* build up a list of all filenames to be used here */
for (i = optind; i < argc; ++i) {
filename = zalloc(sizeof(*filename));
if (!filename)
return -ENOMEM;
filename->filename = strdup(argv[i]);
filename->basename = my_basename(filename->filename);
dbg("adding filename '%s'\n", argv[i]);
list_add_tail(&filename->entry, &session->files_upload);
}
retval = smug_login(session);
if (retval) {
fprintf(stderr, "Can not login\n");
return -1;
}
dbg("1\n");
retval = smug_get_albums(session);
if (retval) {
fprintf(stderr, "Can not read albums\n");
return -1;
}
retval = generate_md5s(&session->files_upload);
if (retval) {
fprintf(stderr, "Error calculating md5s\n");
return -1;
}
album = select_album(album_title, category_title, quicksettings_name,
session);
if (!album)
return -1;
retval = upload_files(session, album);
if (retval) {
fprintf(stderr, "Error uploading files\n");
return -1;
}
smug_logout(session);
session_free(session);
exit:
return 0;
}