# Copyright 2014 Andrea Micheli and Marco Gario
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import

import os
import sys
import platform

from pysmt.cmd.installers.base import SolverInstaller, TemporaryPath


class Z3Installer(SolverInstaller):

    SOLVER = "z3"

    def __init__(self, install_dir, bindings_dir, solver_version,
                 mirror_link=None, osx=None):
        arch = self.architecture
        if arch == "x86_64":
            arch = "x64"

        system = self.os_name
        if system == "linux":
            system = "ubuntu-14.04"
        elif system == "darwin":
            system = "osx-%s" % osx

        archive_name = "z3-%s-%s-%s.zip" % (solver_version, arch, system)
        native_link = "https://github.com/Z3Prover/z3/releases/download/z3-4.4.1/{archive_name}"

        SolverInstaller.__init__(self, install_dir=install_dir,
                                 bindings_dir=bindings_dir,
                                 solver_version=solver_version,
                                 archive_name=archive_name,
                                 native_link=native_link,
                                 mirror_link=mirror_link)

    def move(self):
        bpath = os.path.join(self.extract_path, "bin")
        files = ["z3consts.py",
                 "z3core.py",
                 "z3num.py",
                 "z3poly.py",
                 "z3printer.py",
                 "z3.py",
                 "z3rcf.py",
                 "z3test.py",
                 "z3types.py",
                 "z3util.py"]
        if self.os_name == "linux":
            files += [ "libz3.so" ]
        elif self.os_name == "darwin":
            files += [ "libz3.a", "libz3.dylib" ]
        for f in files:
            SolverInstaller.mv(os.path.join(bpath, f), self.bindings_dir)

    def get_installed_version(self):
        with TemporaryPath([self.bindings_dir]):
            version = None
            try:
                import z3
                (major, minor, ver, _) = z3.get_version()
                version = "%d.%d.%d" % (major, minor, ver)
            finally:
                if "z3" in sys.modules:
                    del sys.modules["z3"]
                # Return None, without raising an exception
                # pylint: disable=lost-exception
                return version

@@ -964,16 +964,35 @@
 %22x86_64%22
+ or arch == %22AMD64%22
 :%0A      
@@ -1175,16 +1175,77 @@
 s%22 %25 osx
+%0A        elif system == %22windows%22:%0A            system = %22win%22
 %0A%0A      
@@ -2307,24 +2307,115 @@
 z3.dylib%22 %5D%0A
+        elif self.os_name == %22windows%22:%0A            files += %5B %22libz3.dll%22, %22libz3.lib%22 %5D%0A%0A
         for 
@@ -2656,16 +2656,24 @@
 mport z3
+        
 %0A       
@@ -3021,12 +3021,13 @@
 urn version%0A
+%0A

Update installer for z3 on windows