#!/usr/bin/python

import xf86config
import os
import sys

mousedev = ""
if os.access("/dev/mouse", os.O_RDONLY):
    mousedev = os.readlink("/dev/mouse")
    if mousedev.endswith("psaux"):
    os.unlink("/dev/mouse")
    os.symlink("/dev/input/mice","/dev/mouse")
    os.system("mouseconfig --noui generic3ps/2")

needsFixed = 0
foundZ = 0

(xconfig, xconfigpath) = xf86config.readConfigFile()
if not xconfig:
    sys.exit(0)
Xmouse = xf86config.getCorePointer(xconfig)
if not Xmouse:
    sys.exit(0)

for o in Xmouse.options:
    if o.name == "Device" and o.val == "/dev/psaux":
    o.val = "/dev/input/mice"
    needsFixed = 1
    if o.name == "Device" and o.val == "/dev/mouse" and mousedev.endswith("psaux"):
    o.val = "/dev/input/mice"
    needsFixed = 1
    if o.name == "Protocol":
    o.val = "IMPS/2"
    if o.name == "ZAxisMapping":
    foundZ = 1
if not foundZ:
    option = xf86config.XF86Option("ZAxisMapping\" \"4 5")
    Xmouse.options.insert(option)

if needsFixed:
    xconfig.write(xconfigpath)
sys.exit(0)