###RUN_PERL: #!/usr/bin/perl

# /bsta/b
# bbcode.pl is generated from bbcode.1.pl.
#
# The bbcode interface
#
# Copyright (C) 2017, 2023, 2024  Balthasar Szczepański
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use utf8;
# use Encode::Locale ('decode_argv');
use Encode ('encode', 'decode');

###PERL_LIB: use lib /botm/lib/bsta
use botm_common (
	'HTTP_STATUS',
	'read_header_env',
	'read_data_file',
	'url_query_decode',
	'join_path',
	'merge_url',
	'http_header_status'
);
use bsta_lib (
	'STATE',
	'fail_method', 'fail_content_type',
	'get_frame', 'get_password',
	'merge_settings',
	'eval_bb', 'bb_to_bbcode'
);

###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /

###PERL_CGI_PATH:           CGI_PATH           = /bsta/
###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v

###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta/
###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state

###PERL_SCHEME:             SCHEME             = http
###PERL_WEBSITE:            WEBSITE            = 1190.bicyclesonthemoon.info

binmode STDIN,  ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
# decode_argv();

my $time = time();
srand ($time-$$);

my %http;
my %cgi;
my %frame_data;
my %default;
my %settings;
my %state;

my $method;
my $frame;
my $password;
my $password_ok;
my $access;
my $ongtime;
my $ong_state;
my $last_frame;
my $frame_data_path;
my $frame_file;

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;

if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
	$method = $1;
}
else{
	exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
}

%http = read_header_env(\%ENV);
%cgi = url_query_decode($ENV{'QUERY_STRING'});

if ($method eq 'POST') {
	if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
		my %cgi_post = url_query_decode( <STDIN> );
		%cgi = merge_settings(\%cgi, \%cgi_post);
	}
	# multipart not supported
	else{
		exit fail_content_type($method, $http{'content-type'});
	}
}

$frame  = get_frame(\%cgi);
$password = get_password(\%cgi);

%settings   = read_data_file(DATA_SETTINGS_PATH());
%default    = read_data_file(DATA_DEFAULT_PATH());
%state      = read_data_file(DATA_STATE_PATH());

$ong_state  = int($state{'state'});
$last_frame = int($state{'last'});

$password_ok = ($password eq $settings{'password'});

if ($frame < 0) {
	$frame = $last_frame + $frame +1;
}
$frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
%frame_data = read_data_file($frame_data_path);
%frame_data = merge_settings(\%default, \%frame_data);

if (
	$password_ok || (
		($ong_state >= STATE->{'waiting'}) &&
		($frame <= $last_frame) &&
		($frame >= 0)
	)
) {
	$access = 1;
}
else {
	$access=0;
	%frame_data = read_data_file(DATA_NOACCESS_PATH());
	%frame_data = merge_settings(\%default, \%frame_data);
}
if ($frame_data{'frame'} ne '') {
	$frame_file = $frame_data{'frame'};
}
else {
	$frame_file = sprintf(
		$settings{'frame'},
		$frame, $frame_data{'ext'}
	);
}

print "Content-type: text/plain; charset=UTF-8\n";
if(!$access) {
	print http_header_status(HTTP_STATUS->{'forbidden'});
}
print "\n";
if($method eq 'HEAD') {
	exit;
}

my $viewer_url = merge_url(
	{'scheme' => SCHEME(), 'host' => WEBSITE()},
	{'path' => CGI_VIEWER_PATH()},
	{'path' => $frame}
);
my $frame_url = merge_url(
	{'scheme' => SCHEME(), 'host' => WEBSITE()},
	{'path' => CGI_PATH()},
	{'path' => $frame_file}
);
my $content = bb_to_bbcode(
	eval_bb(
		$frame_data{'content'},
		1
	)
);

print '[quote][center][size=200]'.$frame_data{'title'}.'[/size]'."\n";
print '[url='.$viewer_url.'][img]'.$frame_url.'[/img][/url][/center]'."\n";
print $content.'[/quote]'."\n";
