b6876d5036ef22fe6d323c93ae656f4604f464fc
[ssic.git] / src / ssic.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long;
7 use CGI::SSI;
8
9 sub main
10 {
11         my %opts;
12         my $input;
13         my $output;
14
15         $SIG{'__WARN__'} = \&warning;
16
17         Getopt::Long::Configure("no_ignore_case", "bundling", "gnu_compat",
18                 "no_getopt_compat");
19         if (not GetOptions(\%opts,
20                         "o=s",
21                         "h|help",
22                         "V|version",
23                 )) {
24                 usage(*STDERR);
25                 exit(4);
26         }
27
28         if (exists($opts{'h'})) {
29                 help(*STDERR);
30                 exit(0);
31         }
32         if (exists($opts{'V'})) {
33                 version(*STDERR);
34                 exit(0);
35         }
36
37         if ($#ARGV lt 0) {
38                 error(4, "No input files\n");
39         }
40         if (exists($opts{'o'})) {
41                 if ($#ARGV gt 0) {
42                         error(4, "Cannot specify -o with multiple files\n");
43                 }
44                 compile($ARGV[0], $opts{'o'});
45         } else {
46                 for $input (@ARGV) {
47                         $output = $input;
48                         $output =~ s/\.[^.]+$/.html/;
49                         compile($input, $output);
50                 }
51         }
52 }
53
54 sub usage
55 {
56         my ($fh) = @_;
57
58         printf($fh "Usage: %s [-o <output>] <input> ...\n", $0);
59 }
60
61 sub help
62 {
63         my ($fh) = @_;
64
65         usage($fh);
66         print("Options:\n");
67         print("  -o <output>    Place the output into <output>\n");
68         print("  -h, --help     Display this information\n");
69         print("  -V, --version  Display compiler version information\n");
70 }
71
72 sub version
73 {
74         my ($fh) = @_;
75
76         print("ssic 0.1.0\n");
77         print("Copyright (C) 2013 Patrick \"P. J.\" McDermott\n");
78         print("License GPLv3+: GNU GPL version 3 or later " .
79                 "<http://gnu.org/licenses/gpl.html>.\n");
80         print("This is free software: you are free to change and " .
81                 "redistribute it.\n");
82         print("There is NO WARRANTY, to the extent permitted by law.\n");
83 }
84
85 sub warning
86 {
87         my ($fmt, $args) = @_;
88
89         printf("ssic: Warning: " . $fmt, $args);
90 }
91
92 sub error
93 {
94         my ($status, $fmt, $args) = @_;
95
96         printf("ssic: Error: " . $fmt, $args);
97         exit($status);
98 }
99
100 sub compile
101 {
102         my ($input, $output) = @_;
103         my $input_fh;
104         my $output_fh;
105         my $ssi;
106
107         if ($input eq $output) {
108                 error(4, "Input and output files are equal\n");
109         }
110
111         open($input_fh, "<", $input);
112         open($output_fh, ">", $output);
113
114         $CGI::SSI::DEBUG = 0;
115         $ssi = CGI::SSI->new();
116
117         print($output_fh $ssi->process(<$input_fh>));
118 }
119
120 main();