#!/usr/bin/python
# yum -- Yum rpm-updating Client (Yellowdog Updater Modified)
# 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 Library 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.
# Copyright 2002 Duke University 


import sys
import os
sys.path.insert(1,'/usr/share/yum')
import yummain
from i18n import _
import yumlock

uid = os.geteuid()
lockfile = '/var/run/yum.pid'
mypid = str(os.getpid())

if uid == 0:
    #check out/get the lockfile
    if yumlock.lock(lockfile, mypid, 0644):
        pass
    else:
        fd = open(lockfile, 'r')
        try: oldpid = int(fd.readline())
        except ValueError:
            # bogus data in the pid file. Throw away.
            os.unlink(lockfile)
        else:
            try: os.kill(oldpid, 0)
            except OSError, e:
                import errno
                if e[0] == errno.ESRCH:
                    # The pid doesn't exist
                    os.unlink(lockfile)
                else:
                    # Whoa. What the heck happened?
                    print _('Unable to check if PID %s is active') % oldpid
                    sys.exit(1)
            else:
                # Another copy seems to be running.
                msg = _('Existing lock %s: another copy is running. Aborting.')
                print msg % lockfile
                sys.exit(1)
        # lock again.
        yumlock.lock(lockfile, mypid, 0644)

try:
    yummain.main(sys.argv[1:])
except SystemExit, e:
    if uid == 0:
        yumlock.unlock(lockfile)
    sys.exit(e)
except KeyboardInterrupt, e:
    print _('Exiting on User Cancel')
    if uid == 0:
        yumlock.unlock(lockfile)
    sys.exit(1)
except IOError, (errno, strerror):
    print _('IOError - # %s - %s') % (errno, strerror)
    if uid == 0:
        yumlock.unlock(lockfile)
    sys.exit(1)


