Make a licensing decision: it's all AGPLv3+.
[odin-cgi] / bin / populate-lang-table
CommitLineData
be24e9af 1#! /usr/bin/perl
128543b0
MW
2###
3### Setup program for Odin services
4###
5### (c) 2015 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the `odin.gg' service, `odin-cgi'.
11###
12### `odin-cgi' is free software; you can redistribute it and/or modify
13### it under the terms of the GNU Affero General Public License as
14### published by the Free Software Foundation; either version 3 of the
15### License, or (at your option) any later version.
16###
17### `odin-cgi' is distributed in the hope that it will be useful,
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20### GNU Affero General Public License for more details.
21###
22### You should have received a copy of the GNU Affero General Public
23### License along with `odin-cgi'; if not, see
24### <http://www.gnu.org/licenses/>.
be24e9af
MW
25
26use lib "lib";
27use Odin;
28
29my $db = Odin::open_db;
30my %newlang, %oldlang;
31
32open my $fh, "-|", "highlight", "-p" or die "highlight: $!";
33while (<$fh>) {
34 my ($descr, $lang) = /^(.*\S)\s*:\s*(\S+)(?:\s.*|)$/;
969db1ef
MW
35 next if !defined $lang or $lang eq "txt";
36 $newlang{"hl:$lang"} = $descr;
be24e9af
MW
37}
38close $fh or die "close highlight: $! $?";
969db1ef 39$newlang{"txt"} = "Plain text";
bc6d34b0 40$newlang{"md"} = "Markdown";
be24e9af
MW
41
42Odin::xact {
43 my $h = $db->selectall_hashref
44 ("SELECT lang, descr FROM odin_pastebin_lang", "lang");
45 for my $k (keys %$h) { $oldlang{$k} = $h->{$k}{descr}; }
46 for my $lang (keys %oldlang) {
47 if (!exists $newlang{$lang}) {
48 print ";; delete stale language `$lang' (`$oldlang{$lang}')\n";
49 $db->do("DELETE FROM odin_pastebin_lang WHERE lang = ?", undef, $lang);
50 }
51 }
52 for my $lang (keys %newlang) {
53 if (!exists $oldlang{$lang}) {
54 print ";; insert new language `$lang' (`$newlang{$lang}')\n";
795ac774
MW
55 $db->do("INSERT INTO odin_pastebin_lang (lang, descr) VALUES (?, ?)",
56 undef, $lang, $newlang{$lang});
be24e9af
MW
57 } elsif ($oldlang{$lang} ne $newlang{$lang}) {
58 print ";; change description for `$lang' ",
59 "(`$oldlang{$lang}' -> `$newlang{$lang}')\n";
60 $db->do("UPDATE odin_pastebin_lang SET descr = ? WHERE lang = ?",
61 undef, $newlang{$lang}, $lang);
62 }
63 }
64} $db;