aboutsummaryrefslogtreecommitdiffstats
path: root/.local/bin/fossil-update
blob: d724145f9bf033b4f529c29416da7cce63e14f0b (plain) (blame)
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
#!/bin/sh

SUDO=doas
FOSSIL_CO=$(fossil all ls -c | grep fossil) # fossil checkout location
ASK_UPGRADE=1
ASK_ALL=1
CLEAN_BUILD=0

usage() {
    echo "usage: fossil-update [OPTIONS]"
    echo "options:"
    echo "  -y  upgrade without asking"
    echo "  -a  upgrade and turn off the fossil service(if it's running) without asking"
    echo "  -c  clean build, run 'make distclean' before building"
    echo "  -h  print this help message"
}

check_service() {
    _fsl_pid=$(pgrep -u root -f $(which fossil)) && export _fsl_pid
    if [[ -n $_fsl_pid ]]; then
        if (( $ASK_ALL )); then
            echo -n "fossil service is active. stop it? (Y/y) "
            read _stop_fsl
            case $_stop_fsl in
                [Yy] ) stop_service
                    ;;
                * ) echo "upgrade is not possible" && exit 2
                    ;;
            esac
        fi
        stop_service
    fi
}

stop_service() {
    # i am using systemd service to run fossil server
    $SUDO systemctl stop fossil
}

compile() {
    echo "configuring..." && \
        ./configure >/dev/null && \
        echo "building..." && make -j8 >/dev/null && echo "build done"
}

install() {
    check_service
    $SUDO make install >/dev/null && echo "upgrade done"
    [[ -n $_fsl_pid ]] && echo "don't forget to enable fossil service back"
}

main() {
    cd $FOSSIL_CO
    if (( $CLEAN_BUILD )); then
        echo "cleaning previous build"
        [ -f Makefile ] && make distclean >/dev/null
    fi
    fossil up trunk
    if (( $ASK_ALL )) && (( $ASK_UPGRADE )); then
        echo -n "upgrade? (Y/y) " && \
            read _upgrade && \
            case $_upgrade in
                [Yy] ) compile && install
                    ;;
                * ) exit 1
                    ;;
            esac
    fi
    compile && install
}

while getopts "acyh" opts
do
    case "${opts}" in
        y)
            ASK_UPGRADE=0
            ;;
        a)
            ASK_ALL=0
            ;;
        c)
            CLEAN_BUILD=1
            ;;
        h|*)
            usage && exit 1
            ;;
    esac
done

main