summarylogtreecommitdiffstats
path: root/archlinux-clang-format
blob: d00b6f76abbed4e21da831e6db61feef0847ead2 (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
#!/usr/bin/env bash
pushd /opt/clang-format-static >/dev/null

set -euo pipefail

print_help()
{
printf "Usage: $@ [OPTION]...
Configure clang-format static binaries

Available options
  status                    List available clang-format binaries
  get                       Display path of current default clang-format
  set <BINARY_NAME>         Create /opt/clang-format-static/clang-format symlink to one of available binaries
"
  exit 0
}

get_available_binaries()
{
    ls -v1 clang-format-*
    exit 0
}

get_current_binary()
{
    if ! [[ -f clang-format && -L clang-format ]]; then
        echo "$PWD/clang-format does not exist or is not a symlink, clang-format is unset"
        exit 1
    fi
    WHICH_PATH=$(which clang-format 2>/dev/null)
    if [ $? -eq 1 ]; then
        echo "which clang-format failed, is \"$SELF_PATH\" in path?"
        exit 1
    fi

    CLANG_FORMAT_FULL_PATH="$PWD/clang-format"
    if [ "$WHICH_PATH" != "$CLANG_FORMAT_FULL_PATH" ]; then
        echo "PATH variable points to \"$WHICH_PATH\" instead of \"$CLANG_FORMAT_FULL_PATH\""
        echo "Is PATH set correctly?"
        echo "Try:"
        echo "export PATH=\"$PWD:\$PATH\""
        exit 1
    fi
    readlink clang-format
    exit 0
}

set_current_binary()
{
    if [[ -f clang-format && ! -L clang-format ]]; then
        echo "$PWD/clang-format is a file, but not a symlink. Please remove this file manually and retry."
        exit 1
    fi


    FILE_NAME="$1"
    if [ ! -f "$FILE_NAME" ]; then
        echo "Target $FILE_NAME does not exist, run \"archlinux-clang-format status\" to see available options."
        exit 1
    fi

    rm -f clang-format
    ln -s "$FILE_NAME" clang-format
    exit 0
}

log_and_exit()
{
    echo "Error: $*"
    exit 1
}

while [[ $# -gt 0 ]] ; do
    case "$1" in
        get) get_current_binary ; shift ;;
        -h|--help|help) print_help "$0" ; shift ;;
        set)
            case "$2" in
                "") print_help "$0";;
                *) set_current_binary "$2" ; shift 2 ;;
            esac ;;
       status) get_available_binaries ; shift ;;
       --) shift ; break ;;
       *) log_and_exit "Invalid argument $1" ;;
    esac
done

print_help "$0"