1 |
/*************************************************************************** |
2 |
* Copyright (C) 2006 by Torben H. Nielsen * |
3 |
* torben@t-hoerup.dk * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the * |
17 |
* Free Software Foundation, Inc., * |
18 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 |
***************************************************************************/ |
20 |
|
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
24 |
#endif |
25 |
|
26 |
#include <stdio.h> |
27 |
#include <stdlib.h> |
28 |
|
29 |
#include "daemon.h" |
30 |
#include "dblayer.h" |
31 |
#include "dlworker.h" |
32 |
|
33 |
#define VERSION "0.1" |
34 |
#define AUTHOR "Torben H. Nielsen" |
35 |
|
36 |
downloadd_config *CONFIG; |
37 |
|
38 |
void printversion(int terminate) { |
39 |
printf("downloadd - ftp/http download daemon v %s\n", VERSION); |
40 |
printf("(C) 2006 by %s\n\n", AUTHOR); |
41 |
if (terminate) |
42 |
exit(0); |
43 |
} |
44 |
|
45 |
void printusage() { |
46 |
printversion(0); |
47 |
printf("Usage: downloadd [options]...\n"); |
48 |
printf(" Option - description\n\n"); |
49 |
printf(" --help Print this screen\n"); |
50 |
printf(" --version Print version\n"); |
51 |
printf(" --debug Run the program in foreground and all log\n"); |
52 |
printf(" messages are redirected to stdout\n"); |
53 |
printf(" --config <file> Specify which configuration file to use\n"); |
54 |
exit(0); |
55 |
} |
56 |
|
57 |
int main(int argc, char *argv[]) |
58 |
{ |
59 |
dlrecord *record; |
60 |
char *configfile = 0; |
61 |
int i, res; |
62 |
char buf[120]; |
63 |
CONFIG = malloc(sizeof(downloadd_config)); |
64 |
CONFIG->debug = 1; // <- this should change to 0 at a later point |
65 |
|
66 |
for (i=1; i<argc; i++) { |
67 |
if (strcmp(argv[i], "--help") == 0) { |
68 |
printusage(); |
69 |
} else if(strcmp(argv[i], "--version") == 0) { |
70 |
printversion(1); |
71 |
} else if (strcmp(argv[i], "--debug") == 0) { |
72 |
CONFIG->debug = 1; |
73 |
} else if (strcmp(argv[i], "--config") == 0) { |
74 |
i++; |
75 |
if (argv[i]) |
76 |
configfile = argv[i]; |
77 |
else |
78 |
printusage(); |
79 |
} else |
80 |
printusage(); |
81 |
} |
82 |
|
83 |
if (CONFIG->debug) |
84 |
log_message("Starting in debug mode"); |
85 |
|
86 |
read_config_file( configfile ); |
87 |
|
88 |
/* do not daemonize if we are in debug mode */ |
89 |
if (!CONFIG->debug) |
90 |
daemonize(); |
91 |
|
92 |
while (1) { |
93 |
record = malloc(sizeof(dlrecord)); |
94 |
res = nextdownload( record ); |
95 |
|
96 |
if (res) { |
97 |
sprintf(buf, "Saving %s to %s", record->URL, record->filename); |
98 |
log_message(buf); |
99 |
downloadfile( record ); |
100 |
} else { |
101 |
if (CONFIG->debug) |
102 |
log_message("No URLs to download"); |
103 |
} |
104 |
free(record); |
105 |
sleep(60); |
106 |
} |
107 |
|
108 |
} |