#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention Updater
#  repository delpackage
#
# Copyright 2004-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

import os
import sys
import getopt

from univention.config_registry import ConfigRegistry
import univention.updater.repository as urepo
import univention.updater.tools

def usage(fd=sys.stdout):
	print >>fd, 'univention-repository-delpackage: tool for removing packages from local repository'
	print >>fd, 'copyright (c) 2004-@%@copyright_lastyear@%@ Univention GmbH, Germany'
	print >>fd, ''
	print >>fd, 'Syntax:'
	print >>fd, '  univention-repository-delpackage --file <debian_package> <debian_package>'
	print >>fd, '  univention-repository-delpackage [--help] '
	print >>fd, ''

# parse options
if __name__ == '__main__':

	if len(sys.argv) < 2:
		usage()
		sys.exit(1)

	if sys.argv[1] in ['-h', '-?', '--help']:
		usage()
		sys.exit(0)


	longopts=['file=' ]
	try:
		opts, args=getopt.getopt(sys.argv[1:], '', longopts)
	except getopt.error, msg:
		print msg
		sys.exit(1)

	files=[]
	packages_dir=''
	for opt, val in opts:
		if opt == '--file':
			files.append(val)

	if len(files) > 0:
		for i in args:
			files.append(i)

	if len(files) < 1:
		usage()
		sys.exit(1)

	try:
		lock = univention.updater.tools.updater_lock_acquire()
	except univention.updater.tools.LockingError, e:
		print >>sys.stderr, e
		sys.exit(5)
	try:
		ucr = ConfigRegistry()
		ucr.load()

		if ucr.is_false('local/repository', True):
			print >> sys.stderr, 'Error: The local repository is diabled. Use "univention-config-registry set local/repository=yes" to activate local repository'
			sys.exit(1)

		dirs=[]

		for file in files:
			if file.endswith('.deb') and os.path.exists(file):
				dir, filename = os.path.split( file )
				basedir = urepo.get_repo_basedir( dir )
				if not basedir in dirs:
					dirs.append( basedir )
				os.unlink( file )

		for path in dirs:
			# these might not exist if creating a new repository
			urepo.create_packages( path, '.' )
			if path[ -1 ] == '/':
				path = path[ : -1 ]

			head, tail = os.path.split( path )
			for arch in urepo.ARCHITECTURES:
				urepo.create_packages( head, '%s/%s' % ( tail, arch ) )
	finally:
		if not univention.updater.tools.updater_lock_release(lock):
			print 'WARNING: updater-lock already released!'
