#!/usr/bin/perl

#    set_offline.pl - Set apt-cacher-ng offlinemode option on and off
#    Copyright (C) 2008  Giovanni Mascellani
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

if ((@ARGV < 1) or (@ARGV > 2)) {
	die "I need one (offline mode, \"0\" or \"1\") or two (configuration file name and offline mode) arguments, while you gave me ", int(@ARGV), "!";
}

my $mode, $filename;

if (@ARGV == 1) {
	$mode = $ARGV[0];
	$filename = "/etc/apt-cacher-ng/acng.conf";
} else {
	$mode = $ARGV[1];
	$filename = $ARGV[0];
}

if (($mode ne "0") and ($mode ne "1")) { die "The argument must be '0' or '1'"; }

open(cffile, $filename) or die "Could not open file $filename: $!";
my @configuration = <cffile>;
close(cffile);

open(cffile, ">$filename") or die "Could not open file $filename in write mode: $!";
foreach (@configuration) {
	if (/^offlinemode:/) {
		print cffile "offlinemode:$mode\n";
	} else {
		print cffile;
	}
}
close(cffile);

system("/etc/init.d/apt-cacher-ng", "restart");

