#! /usr/bin/python -OOt
# -*- python -*-
# -*- coding: UTF-8 -*-
#
#  ntfs-config-root : Execute ntfs-config as root. If you are not
#                      already root, search for an authentification program.
#  Copyright (C) 2007 Mertens Florent <flomertens@gmail.com>
#
#  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import os
import sys
import gettext
from subprocess import *
from NtfsConfig import Dialogs
from NtfsConfig import config
from NtfsConfig.Utility import infomsg
from gettext import gettext as _

if __name__ == '__main__' :
    
    gettext.bindtextdomain("ntfs-config",config.localedir)
    gettext.textdomain("ntfs-config")
    if len(sys.argv) > 1 :
        opt = " ".join(sys.argv[1:])
    else :
        opt = ""
    
    if not os.getuid() == 0 :
        if not call(["which gksu"], stdout=PIPE, stderr=PIPE, close_fds=True, shell=True) :
            infomsg("Launching ntfs-config with gksu...")
            auth = "gksu"
            auth_opt = "-k"
        elif not call(["which kdesu"], stdout=PIPE, stderr=PIPE, close_fds=True, shell=True) :
            infomsg("Launching ntfs-config with kdesu...")
            auth = "kdesu"
            auth_opt = ""
        else :
            Dialogs.warning_dial(_("<big><b>No authentification program found</b></big>"), \
                    _("Ntfs-config need to be run as root, but i can't find\n"
                      "any graphical authentification program.\n" 
                      "You should install <i>gksu</i> or <i>kdesu</i>.\n" 
                      "Alternativly, you can run ntfs-config from "
                      "the terminal with <i>su</i> or <i>sudo</i>."), \
                    "", None)
            sys.exit()
        argv = [auth, auth_opt, "--", "ntfs-config", opt]
        os.execvp(auth, argv)
    else :
        argv = ["ntfs-config", opt]
        os.execvp("ntfs-config", argv)
        
        
