#!/usr/bin/env python
# -*- coding: utf-8 -*-
# +--------------------------------------------------------------------------+
# | Endian Firewall                                                          |
# +--------------------------------------------------------------------------+
# | Copyright (c) 2004-2016 S.p.A. <info@endian.com>                         |
# |         Endian S.p.A.                                                    |
# |         via Pillhof 47                                                   |
# |         39057 Appiano (BZ)                                               |
# |         Italy                                                            |
# |                                                                          |
# | 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.              |
# +--------------------------------------------------------------------------+

import sys
import os
from endian.core.daemon import daemonize


def run_detached(cmd):
    daemonize()
    os.system(cmd)


def to_args():
    if len(sys.argv) <= 1:
        return ""
    args = "".join(sys.argv[1:]).split("--")
    del args[0]
    new_args = []
    for arg in args:
        if arg.find('=') > -1:
            key, value = arg.split('=')
            new_args.append("--%s=\"%s\"" % (key, value))
        else:
            new_args.append("--%s" % arg)
    return " ".join(new_args)

run_detached("sudo /usr/local/bin/backup-create.sh %s &>/dev/null" % to_args())
