summaryrefslogtreecommitdiff
path: root/script/install.py
blob: e08f8c514de8a0e5e29fb400dff51cc40cd20cfd (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
#!/usr/bin/env python3

import re
import sys
from pathlib import Path


def install_service_unit(prefix):
    project_path = Path(__file__).parents[1]

    if prefix == "usr":
        install_path = Path("/etc/systemd/system")
    elif prefix == "usr/local":
        install_path = Path("/usr/local/lib/systemd/system")
    else:
        raise Exception("first arg <prefix> must be one of `usr` or `usr/local`")

    def substitute(match):
        if match[1] == "PROJECT_PATH":
            return str(project_path)
        else:
            raise KeyError(match[1])

    service_text_in = open(
        project_path / "script" / "jwebmail.service.in", mode="r"
    ).read()
    service_text = re.sub("@(\w+)@", substitute, service_text_in, flags=re.ASCII)

    install_path.mkdir(0o755, True, True)

    open(install_path / "jwebmail.service", mode="w").write(service_text)


if __name__ == "__main__":
    install_service_unit(sys.argv[1] if len(sys.argv) == 2 else '')