#!/usr/bin/perl
#
# PrivateOn-VPN -- Because privacy matters.
#
# Author: Mikko Rautiainen <info@tietosuojakone.fi>
#
# Copyright (C) 2014-2015  PrivateOn / Tietosuojakone Oy, Helsinki, Finland
# All rights reserved. Use is subject to license terms.
#

#
#		/opt/PrivateOn-VPN/vpn-gui/vpn-gui
#
#   Vpn-gui is the front-end for the vpn-monitor daemon.
#   This application is used to change the selected VPN server.
#   When you want to turn off the VPN, it must be done using the application,
#   otherwise the backend and dispatcher script will reconnect the VPN.
#
#  Note: This program must be run with root privileges, preferably using sudo.
#        This program requires that the vpn-monitor backend daemon is running.
#


use strict;
use warnings;
use QtCore4;
use QtGui4;
use lib "/opt/PrivateOn-VPN/vpn-gui";
use vpn_window;
use vpn_tray;


# Preflight check: is this program being run with root privileges
if ($<) {
	die "\nError:\tThis program must be run with root privileges.\n" .
	    "\tStart program by running \'/opt/PrivateOn-VPN/vpn-gui/launch-vpn-gui.sh\' as a non-root user.\n\n";
}


# process command-line arguments
my $show_option = 0;
foreach (@ARGV) {
	if ( /^(-\?|-h|--help)$/ ) {
		print STDERR "Usage: vpn-gui [OPTION]\n" .
		   "  -s or --show       Show widget on startup\n" .
		   "  -h or --help       Print this message and exit\n";
		exit 0;
	} elsif ( /^(-s|--show)$/ ) {
		$show_option = 1;
	}
}


sub main
{
	my $app = Qt::Application(\@ARGV);
        $app->setQuitOnLastWindowClosed(0);
	my $window = vpn_window();
	my $tray = vpn_tray($window);

	if ($show_option) {
		$window->show();
	}
	return $app->exec();
}

exit main();
