summarylogtreecommitdiffstats
path: root/nquakesv-init.py
blob: c042f8b93d0736830a4d15b22fc4feb8a268f7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3

# Shoutout to VVD on QuakeNet:#qw-dev. It's because of him not understanding why user's homedir
# overrides are a good thing, even for server software, that I even had to write this bullshit.
# Seriously. This entire script wouldn't be necessary if mvdsv just supported directory layering like
# ezquake, darkplaces(-ded), zandronum(-ded), etc. all do. C'mon. Get with the times.

import os
import pwd
import datetime
import json
import subprocess

timestamp = datetime.datetime.now().strftime('%Y.%m.%d_%H%M.%S')
user = pwd.getpwuid(os.geteuid()).pw_name
homedir = os.environ['HOME']
nquakedir = homedir + '/.nquakesv'
pkgdir = '/usr/share/nquakesv'

def getVer():
    try:
        ver = 'unknown'
        pkginfo = subprocess.check_output(['pacman', '-Qi', 'nquakesv']).decode('utf-8').splitlines()
        for line in pkginfo:
            if line.startswith('Version'):
                ver = line.split(':')[1].strip()
                break
    except:
        ver = 'unknown'
    return(ver)

# Probably not actually necessary...
#def symLinker(srcdir, destparent, filext = False):
#    for f in os.listdir(srcdir):
#        # We only want files
#        if not os.path.isfile('{0}/{1}'.format(srcdir, f)):
#            continue
#        if filext:
#            if f.endswith(filext):
#                os.symlink('{0}/{1}'.format(srcdir, f),
#                           '{0}/{1}'.format(destparent, f))
#        else:
#            os.symlink('{0}/{1}'.format(srcdir, f),
#                       '{0}/{1}'.format(destparent, f))

def populateTree():
    # First we'll do the id1/ dir. It's easy enough.
    os.symlink('{0}/id1/pak0.pak.DEMO'.format(pkgdir), '{0}/id1/pak0.pak'.format(nquakedir))
    # Sometimes other games/engines/whatever might be installed that require the retail pak placed here.
    if os.path.isfile('/opt/quake/id1/pak1.pak'):
        os.symlink('/opt/quake/id1/pak1.pak', '{0}/id1/pak1.pak'.format(nquakedir))
    # Now the bulk of it.
    for rootdir in ('ktx', 'qw', 'qtv'):
        for root, dirs, files in os.walk('{0}/{1}'.format(pkgdir, rootdir)):
            destpath = root.replace(pkgdir, nquakedir)
            for f in files:
                os.symlink('{0}/{1}'.format(root, f),
                           '{0}/{1}'.format(destpath, f))

def buildRootTree(ver = False):
    # We dont need these dirs.
    # Matches "greedily"- that is, "addons" will match e.g. addons/somedir/someotherdir
    # So be as explicit as possible.
    direxcludes = ['addons', 'run']
    # And these should be links to /usr/share/nquakesv/<name>
    # These, unlike the above, are *inherently* explicit- the path must be exact.
    dirlinks = []
    if ver == False:
        ver = getVer()
    # Overwriting is bad, mmk?
    if os.path.isdir(nquakedir):
        os.rename(nquakedir, '{0}.bak_{1}'.format(nquakedir, timestamp))
    os.makedirs(nquakedir, exist_ok = True)
    with open(nquakedir + '/VERSION', 'w') as f:
        f.write('Generated on nquakesv package version {0}\n'.format(ver))
    # Generated from a vanilla from-"source" nquakesv install
    # and generated by running the following inside the directory:
    #   find ./ -type d -printf "%P\n" | sed -e '/^$/d' | sort
    with open('{0}/dirtree.lst'.format(pkgdir), 'r') as raw:
        dirtree = raw.read().splitlines()
    # Generated from a vanilla from-"source" nquakesv install
    # and generated by running the following inside the directory:
    #   for l in $(find ./ -type l); do echo -n "${l} " | sed -re "s@^\./@@g"; readlink ${l}; done
    with open('{0}/linktree.lst'.format(pkgdir), 'r') as raw:
        linktree = raw.read().splitlines()
    # Walk the paths and build the tree.            
    for d in dirtree:
        makeme = True
        for e in direxcludes:
            if d.startswith(e):
                makeme = False
                break
        if makeme == True and d in dirlinks:
            os.symlink('{0}/{1}'.format(pkgdir, d),
                       '{0}/{1}'.format(nquakedir, d))
        elif makeme == True:
            os.makedirs('{0}/{1}'.format(nquakedir, d), exist_ok = True)
    for l in linktree:
        src = l.split()[1]
        dest = l.split()[0]
        makeme = True
        for e in direxcludes:
            if dest.startswith(e):
                makeme = False
                break
        if makeme == True:
            destpath = '{0}/{1}'.format(nquakedir, dest)
            if not os.path.isdir(os.path.dirname(destpath)):
                os.makedirs(os.path.dirname(destpath))
            os.symlink(src, destpath)

def main():
    buildRootTree()
    populateTree()

if __name__ == '__main__':
    main()