#!/usr/bin/perl
##
##

use CGI;
$CGI::LIST_CONTEXT_WARN = 0;

$q = new CGI;
print $q->header(-charset=> "UTF-8",);
$rxOffset = 0;
$txOffset = 0;
open(DvmegaFILE, "< /var/www/dvmega.conf") or die("Error: $!");
my $match_pattern = '^\s*(#.*|)$';
while (my $line = <DvmegaFILE>) {
        chomp $line;
        if ($line !~ $match_pattern) {
		if (substr($line , 0, 16) eq "DVMEGA_FREQUENCY") {
			my @list = split (/=/, $line);
			$Freq = pop @list;
		}
		if (substr($line , 0, 15) eq "DVMEGA_TX_LEVEL") {
			my @list = split (/=/, $line);
			$txLevel = pop @list;
		}
		if (substr($line , 0, 15) eq "DVMEGA_TX_DELAY") {
			my @list = split (/=/, $line);
			$txDelay = pop @list;
		}
		if (substr($line , 0, 15) eq "DVMEGA_RX_LEVEL") {
			my @list = split (/=/, $line);
			$rxLevel = pop @list;
		}
		if (substr($line , 0, 13) eq "DVMEGA_DEVICE") {
			my @list = split (/=/, $line);
			$device = pop @list;
		}
                if (substr($line , 0, 16) eq "DVMEGA_TX_OFFSET") {
                        my @list = split (/=/, $line);
                        $txOffset = pop @list;
                }
                if (substr($line , 0, 16) eq "DVMEGA_RX_OFFSET") {
                        my @list = split (/=/, $line);
                        $rxOffset = pop @list;
                }
        }
    }
close (DvmegaFILE);
print <<'EOF';
<HTML lang="ja">
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META http-equiv="Content-Style-Type" content="text/css">
<script>
function Store() {
if (((dvmega_set.freq.value >= 144000000) && (dvmega_set.freq.value <= 146000000))  || (( dvmega_set.freq.value >= 430000000) && (dvmega_set.freq.value <= 440000000))) { 
document.dvmega_set.action="/cgi-bin/DvmegaStore";
document.dvmega_set.target="_top";
document.dvmega_set.method="post";
document.dvmega_set.submit();
} else {
alert ('周波数の値が間違っています');
}
}
</script>
<TITLE>DVMEGA Setting</TITLE>
</HEAD>
<BODY>
<form name="dvmega_set">
<br>DVMEGA設定</br>
EOF
print "周波数 <input type=\"text\" name=\"freq\" value=\"" . $Freq . "\" size=\"10\">Hz<br>\n";
print "送信周波数のオフセット<input type=\"text\" name=\"txOffset\" value=\"" . $txOffset . "\" size=\"5\">Hz<br>\n";
print "受信周波数のオフセット<input type=\"text\" name=\"rxOffset\" value=\"" . $rxOffset . "\" size=\"5\">Hz<br>\n";
print "受信レベル <input type=\"text\" name=\"rxLevel\" value=\"" . $rxLevel . "\" size=\"4\"><br>\n";
print "送信レベル <input type=\"text\" name=\"txLevel\" value=\"" . $txLevel . "\" size=\"4\"><br>\n";
print "送信遅延 <input type=\"text\" name=\"txDelay\" value=\"" . $txDelay . "\" size=\"4\">ミリ秒<br>\n";
print "接続デバイス名<input type=\"text\" name=\"device\" value=\"" . $device . "\" size=\"10\"><br>\n";
print <<'EOF';
<button type="button" onclick="Store()">登録</button><br>
登録をクリックしますと設定を保存後、ラズベリーパイが再起動されます。
</form>
EOF
print $q->end_html;

