#!/usr/bin/python
# Initializes pbuilder environment

# 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

DIRS=('builds',DIST+"-debs",'src')
for i in DIRS:
  os.system('mkdir -p /var/cache/prevu/'+i)
  os.system('chown root:admin /var/cache/prevu/'+i)
  os.system('chmod 775 /var/cache/prevu/'+i)
  
os.chdir('/var/cache/prevu/%s-debs' % DIST)
os.system('dpkg-scanpackages . /dev/null > Packages 2>/dev/null')
os.system('chown root:admin Packages')
os.system('chmod 775 Packages')
ret=os.system('pbuilder create --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" --distribution %s' % (DIST,DIST,DIST,DIST,DIST,DIST,DIST,DIST,DIST) )
if ret:
  print "pbuilder sub-process failed!"
  sys.exit(-1)
os.system('chmod 664 /var/cache/prevu/%s.tgz' % DIST)
if DIST == os.popen('lsb_release -c').read().split(':')[1].strip() and not os.popen('cat /etc/apt/sources.list | grep -v ^# | grep "/var/cache/prevu/%s-debs"' % DIST).read():
  print "You can have APT use the packages built by prevu by adding this to /etc/apt/sources.list:"
  print "\tdeb file:/var/cache/prevu/%s-debs ./" % DIST
  print "Would you like prevu to do this for you? [Y/N] "
  i=raw_input()
  while not i in ('Y','y','N','n'):
    i=raw_input("[Y/N]> ")
  if i in ('Y','y'):
    os.system('(echo && echo "#Added by prevu" && echo deb file:/var/cache/prevu/%s-debs ./) >> /etc/apt/sources.list' % DIST)
    os.system('apt-get update')
print "prevu-init done!"
