#!/usr/bin/python
import os
"""
(1) rename original file to -annotated.
(2) ln -s to wine's windows path
(3) cd to windows path (why necess??) and launch wine app

(June 2011): I've made it create a backup, unannotated version too: tmp safety feature. Oops! Not temporary!! It seems PDFXCview can lose front/header pages and in that case messes up page numbering!!! Sigh... Where's the open source PDF editer!?

"""
import sys
def doSys(ss):
    print ss
    os.system(ss)

assert len(sys.argv)==2
pdff=sys.argv[1]
assert pdff.endswith('.pdf')
pdff=pdff[:-4] # Strip ".pdf"
folder,fn=os.path.split(pdff)
absfolder=os.path.abspath(folder) # Need full path for ln -s command

if not pdff.endswith('-annotated'):
    #print "cp -a %s/%s.pdf %s/%s-annotated.pdf"%(absfolder,fn,absfolder,fn)
    os.system( "cp -a  %s/%s.pdf %s/tmp-bk-%s.pdf"%(absfolder,fn,absfolder,fn))
    os.system( "mv  %s/%s.pdf %s/%s-annotated.pdf"%(absfolder,fn,absfolder,fn))
    fn+='-annotated'
    pdff+='-annotated'

rawfile=absfolder+'/'+fn+'.pdf'
linkfile='~/.wine/drive_c/'+fn+'.pdf'


if not os.path.exists(linkfile) and os.path.islink(linkfile):
    print ' This file is linked in .wine by a broken link: '+linkfile
    fooo

if not os.path.exists(linkfile):
    doSys("ln -s "+rawfile+' '+linkfile)
else:
    pass
    # print 'Seems to exist already: '+linkfile
doSys("gnome-open "+linkfile+" &") # Why?! Because you can get started while wine is loading up... :(
doSys("cd ~/.wine/drive_c && wine ~/.wine/drive_c/ProgramFiles/TrackerSoftware/PDF\ Viewer/PDFXCview.exe "+fn+'.pdf  & ')

