#! /usr/bin/python3
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Inuvika Inc.
# http://www.inuvika.com
#
# Copyright (C) 2009-2012 Ulteo SAS
# http://www.ulteo.com
# Author Gauvain POCENTEK <gauvain@ulteo.com> 2009
# Author Julien LANGLOIS <julien@ulteo.com> 2009, 2010, 2012
# Author Laurent CLOUET <laurent@ulteo.com> 2010
#
# 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; version 2
# of the License
#
# 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 hashlib
import os
import sys

from ovd_shells.Logger import Logger
from ovd_shells.Platform import _platform as Platform
from ovd_shells import Util

def main():
	path = os.path.join(os.environ['OVD_SESSION_DIR'], "client_app")
	if not os.path.exists(path):
		Logger.error("no such directory %s"%(path))
		sys.exit(1)

	if len(sys.argv) < 3:
		Logger.error("no file given or missing application id")
		sys.exit(1)

	appid = sys.argv[1]
	filepath = sys.argv[2]
	if Platform.hasProfile():
		filepath = Platform.getVFSPath(filepath)

	hash = hashlib.md5(filepath.encode()).hexdigest()

	open(os.path.join(path, f'{appid}-{hash}'), 'w').write(filepath)
	sys.exit(0)

if __name__ == "__main__":
	user_sdir = Util.get_session_dir()
	if user_sdir is None:
		sys.exit(1)

	Util.redirect_to_dump(user_sdir)
	Logger.init()
	main()
