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