summaryrefslogtreecommitdiff
path: root/make-links.py
blob: a0921a9f2f3560ccf5d78125b9d111996f334341 (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
#!/usr/bin/env python3
import os
import sys

import glob
import tty
import termios
import shutil


def getchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
        sys.stdout.write(ch)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


def make_link(filename, target, prefix='.'):
    orig = os.path.abspath(filename)
    dest = os.path.join(target, '%s%s' % (prefix, filename))
    create_link(filename, orig, dest)

def make_firefox_link(filename, target):
    orig = os.path.abspath(filename)

    profiles = glob.glob(os.path.join(target, '.mozilla/firefox/*.default'))
    if len(profiles) != 1:
        print("Could not find Firefox default profile")
        return
    dest = os.path.join(profiles[0], os.path.basename(filename))
    create_link(filename, orig, dest)

def create_link(filename, orig, dest):
    needs_removal = False
    if os.path.islink(dest):
        if os.readlink(dest) == orig:
            # print('%s already exists.' % dest)
            return
        else:
            print('%s points to: %s' % (dest, os.readlink(dest)))
            needs_removal = True
    elif os.path.isdir(dest):
            print('%s exists and is a directory.' % dest)
            needs_removal = True
    elif os.path.exists(dest):
            print('%s exists and is not a link.' % dest)
            needs_removal = True
    if needs_removal:
        print('Replace? y/[n] ', end='', flush=True)
        chr = getchar()
        print("")
        if (chr == 'y') or (chr == 'Y'):
            if os.path.isdir(dest):
                shutil.rmtree(dest)
            else:
                os.unlink(dest)
        else:
            return
    else:
        print("%s does not exist" % dest)
    try:
        print("%s -> %s" % (dest, orig))
        os.symlink(orig, dest)
    except OSError as e:
        print('Could not create link for: %s (%s)' % (filename, e))

if __name__ == '__main__':
    if len(sys.argv) > 1:
        ROOT = sys.argv[1]
    else:
        ROOT = os.environ["HOME"]

    IGNORE_FILES = ["make-links.py"]

    for f in os.listdir('.'):
        if f.startswith('.'):
            continue
        if f in IGNORE_FILES:
            continue

        if f == 'config':
            for cf in os.listdir(f):
                if f.startswith('.'):
                    continue
                make_link(os.path.join('config', cf), ROOT)
        elif f == 'firefox':
            for cf in os.listdir(f):
                make_firefox_link(os.path.join('firefox', cf), ROOT)
        else:
            make_link(f, ROOT, '.')