#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention Corporate Client
#  Tool to install the Citrix Receiver into a UCC image
#
# Copyright (C) 2014-2016 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 optparse, tempfile, subprocess, os, sys, commands

parser = optparse.OptionParser()

# Return codes
# 0 Success
# 1 Insufficient arguments
# 2 Image file not found
# 3 DEB file not found
# 4 Image mount failed
# 5 Installation of Citrix Receiver failed



def chroot_cmd(cmd):
	return subprocess.call('chroot ' + mntpoint + ' ' + cmd, shell=True, stdout=logfile, stderr=logfile)

def cmd(cmd):
	return subprocess.call(cmd, shell=True, stdout=logfile, stderr=logfile)

def log(msg, percent=None):
	print msg
	logfile.write(msg + "\n")
	if opt.progress and percent is not None:
		print '_PROGRESS_: %s' % percent
		sys.stdout.flush()

def exit(msg, exit_code):
	log(msg)
	if opt.progress:
		print '_ERROR_: %s' % msg
		sys.stdout.flush()
	sys.exit(exit_code)

def cleanup():
	log("Cleaning up")

	if os.path.exists(mntpoint):
		subprocess.call('sync', shell=True)
		subprocess.call('umount ' + mntpoint, shell=True)

# if commands.getoutput('dpkg --print-architecture') != 'i386':
# 	print "The installation can only occur on a i386 system"
# 	sys.exit(6)

parser.add_option("-i", "--uccimage", dest="image", action="store", help="The UCC image into which the Debian package should be installed")
parser.add_option("-d", "--debpackage", dest="deb", action="store", help="The Debian package to be installed into the UCC image")
parser.add_option("-p", "--progress", dest="progress", action="store_true", help="Print parsable progress information to stdout")

opt, args = parser.parse_args()

if not opt.image:
	print 'No image was specified: --uccimage'
	sys.exit(1)

if not opt.deb:
	print 'No DEB package was specified: --debpackage'
	sys.exit(1)

if not os.path.exists(opt.image):
	print "Image file " + opt.image + ' not found.'
	sys.exit(2)

if not os.path.exists(opt.deb):
	print "DEB file file " + opt.deb + ' not found.'
	sys.exit(3)

mntpoint=tempfile.mkdtemp()

logfile = open("/var/log/univention/citrix-install.log", "w", 0)
log("Adding Citrix Receiver to the UCC image, see /var/log/univention/citrix-install.log for more details")
log("Mounting image", 2)
if cmd('mount -o loop /var/lib/univention-client-boot/' + os.path.basename(opt.image) + ' ' + mntpoint) != 0:
	exit("Mounting the image failed! Maybe there are no free loop devices.", 4)

# f = open(os.path.join(mntpoint, "check-ica-state.sh"), "w")
# f.write("LC_ALL=C dpkg-query -W  -f='${Status}' icaclient | grep -q installed")
# f.close()
# if chroot_cmd('bash ./check-ica-state.sh') == 0:
# 	log("Citrix Receiver already installed in chroot")
# 	cleanup()
# 	sys.exit(0)

log("Copying Receiver package", 5)
cmd('cp ' + opt.deb + ' ' + mntpoint)

log("Installing dependencies", 10)
chroot_cmd('apt-get update')
chroot_cmd('apt-get --force-yes --assume-yes install univention-ucc-session-xenapp')

log("Installing Citrix Receiver", 50)
if chroot_cmd('dpkg -i --force-confnew --force-overwrite --force-overwrite-dir ' + os.path.basename(opt.deb)) != 0:
	cleanup()
	exit("Installation of Citrix Receiver failed, please result the logfile (%s)." % logfile.name, 5)

chroot_cmd('apt-get clean')
chroot_cmd('sync')
cmd('sync')

cleanup()

log("Generating hash value", 80)
cmd = 'md5sum "%s" | sed -ne "s| .*||p" > "%s" ' % (opt.image, opt.image+".md5")
if subprocess.call(cmd, shell=True):
	exit("Generation of hash value failed", 6)

log("Done", 100)

