#!/usr/bin/python

# Copyright (C) 2006 John Dong <jdong@ubuntu.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import os
import sys
if os.getuid():
  raise OSError("You need to be root!")

#Initialize target distribution version info
#If environment variable DIST is present, we shall use that.
#Else, use lsb_release to check current distro version, and use that.
DIST=os.getenv('DIST') or ""
if not DIST in ('warty','hoary','breezy','dapper','edgy','feisty','gutsy','hardy'):
  # Distro got through environment is not valid, building against running distro
  print >> sys.stderr, "I: Building against currently running distro:",
  DIST=os.popen('lsb_release -c').read().split(':')[1].strip()
  print >> sys.stderr, DIST
else:
  print >> sys.stderr, "I: Building against specified distro:", DIST

os.system('pbuilder login --basetgz /var/cache/prevu/%s.tgz --buildplace /var/cache/prevu/builds --distribution %s --override-config --othermirror "deb http://archive.ubuntu.com/ubuntu %s main restricted universe multiverse | deb http://archive.ubuntu.com/ubuntu %s-backports main restricted universe multiverse | deb http://archive.ubuntu.com/ubuntu %s-security main restricted universe multiverse | deb http://archive.ubuntu.com/ubuntu %s-updates main restricted universe multiverse | deb file:/var/cache/prevu/%s-debs ./" --bindmounts "/var/cache/prevu/%s-debs /tmp" --distribution %s' % (DIST,DIST,DIST,DIST,DIST,DIST,DIST,DIST,DIST) )

