summaryrefslogtreecommitdiff
path: root/local/bin/mksymlink
blob: 54ecd4ccb9244b0de4f75592f376c725d1e14123 (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
#!/bin/sh

CONFIG_FILE="${HOME}/.config/mksymlink.conf"
TARGET_BASE="${HOME}/.config"

usage() {
    echo "Usage: $0 [-c config_file] [-t target_base]"
    exit 1
}

while getopts "c:t:h" opt; do
    case "$opt" in
        c) CONFIG_FILE="$OPTARG" ;;
        t) TARGET_BASE="$OPTARG" ;;
        h) usage ;;
        *) usage ;;
    esac
done

if [ ! -f "$CONFIG_FILE" ]; then
    echo "Error: config file '$CONFIG_FILE' does not exist" 1>&2
    exit 2
fi

. "$CONFIG_FILE"

if [ -z "$MAIN" ]; then
    echo "Error: MAIN is not set in config file" 1>&2
    exit 3
fi

mkdir -p "$TARGET_BASE"

for entry in $list; do
    TARGET=$(echo "$entry" | cut -d= -f1)
    SRC_REL=$(echo "$entry" | cut -d= -f2)
    SRC="$MAIN/$SRC_REL"
    TARGET_PATH="$TARGET_BASE/$TARGET"

    if [ ! -e "$SRC" ]; then
        echo "Warning: source '$SRC' does not exist, skipping" 1>&2
        continue
    fi

    mkdir -p "$(dirname "$TARGET_PATH")"

    if [ -L "$TARGET_PATH" ]; then
        rm "$TARGET_PATH"
    elif [ -e "$TARGET_PATH" ]; then
        echo "Warning: target '$TARGET_PATH' exists and is not a symlink, skipping" 1>&2
        continue
    fi

    ln -s "$SRC" "$TARGET_PATH"
    echo "Created symlink: $TARGET_PATH -> $SRC"
done