1#!/usr/bin/env python3
2
3import os
4import sys
5import argparse
6import platform
7from pathlib import PurePath
8
9if __name__=='__main__':
10    parser = argparse.ArgumentParser()
11    parser.add_argument('availpath')
12    parser.add_argument('confpath')
13    parser.add_argument('links', nargs='+')
14    args = parser.parse_args()
15
16    if os.path.isabs(args.confpath):
17        destdir = os.environ.get('DESTDIR')
18        if destdir:
19            # c:\destdir + c:\prefix must produce c:\destdir\prefix
20            confpath = str(PurePath(destdir, *PurePath(args.confpath).parts[1:]))
21        else:
22            confpath = args.confpath
23    else:
24        confpath = os.path.join(os.environ['MESON_INSTALL_DESTDIR_PREFIX'], args.confpath)
25
26    if not os.path.exists(confpath):
27        os.makedirs(confpath)
28
29    for link in args.links:
30        src = os.path.join(args.availpath, link)
31        dst = os.path.join(confpath, link)
32        try:
33            os.remove(dst)
34        except FileNotFoundError:
35            pass
36        try:
37            os.symlink(os.path.relpath(src, start=args.confpath), dst)
38        except NotImplementedError:
39            # Not supported on this version of Windows
40            break
41        except OSError as e:
42            # Symlink privileges are not available
43            if platform.system().lower() == 'windows' and e.winerror == 1314:
44                break
45            raise
46