summaryrefslogtreecommitdiff
path: root/support/scripts/install.sh
blob: 352b47f75a1e96585e97d36c6095511235e2c99b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
########################################################-*-Shell-script-*-####
# Install Function
##############################################################################
# $Id$

source ${0%/*}/stdlib.sh;

##############################################################################
# install

function install-args() {
    case "$1" in
        --version             ) echo "install (bash script) $version"; exit 0;;
        -d                    ) directory="true"; return 1;;
        --directory           ) directory="true"; return 1;;
        -D                    ) leading="true"; return 1;;
        -o                    ) args-option-value owner "$@";;
        --owner=*             ) args-inline-value owner "$@";;
        -g                    ) args-option-value group "$@";;
        --group=*             ) args-inline-value group "$@";;
        -m                    ) args-option-value mode  "$@";;
        --mode=*              ) args-inline-value mode  "$@";;
        -p                    ) preserve="true"; return 1;;
        --preserve-timestamps ) preserve="true"; return 1;;
        -*                    ) args-option-unknown     "$@";;
        *                     ) args-append-array files "$@";;
    esac;
}

function install-attr() {
    local file="$1";
    if [ -n "$mode" ]; then
        run chmod "$mode" "$file";
    fi;
    if [ -n "$owner" -a -n "$group" ]; then
        run chown "$owner:$group" "$file";
    elif [ -n "$owner" ]; then
        run chown "$owner" "$file";
    elif [ -n "$group" ]; then
        run chown ":$group" "$file";
    fi;
}

function install-mkdir() {
    local dstpath="$1"; shift 1;
    if [ "$dstpath" != "." ]; then
        run mkdir -p "$dstpath";
    fi;
}

function install-copy() {
    local srcfile="$1"; shift 1;
    local dstfile="$1"; shift 1;
    run cp "$srcfile" "$dstfile";
    if [ "$preserve" == "true" ]; then
        touch --reference="$srcfile" "$dstfile";
    fi;
    install-attr "$dstfile";
}

function install-dirs() {
    while [ $# -gt 0 ]; do
        local dstpath="$1"; shift 1;
        install-mkdir "$dstpath";
        install-attr "$dstpath";
    done;
}

function install-file() {
    local srcfile="$1"; shift 1;
    local dstfile="$1"; shift 1;
    local dstpath="`dirname "$dstfile"`";
    if [ "$leading" == "true" ]; then
        install-mkdir "$dstpath";
    fi;
    install-copy "$srcfile" "$dstfile";
}

function install-files() {
    local dstpath="$1"; shift 1;
    while [ $# -gt 0 ]; do
        local srcfile="$1"; shift 1;
        local dstfile="$dstpath/`basename "$srcfile"`";
        install-copy "$srcfile" "$dstfile";
    done;
}

function install() {
    local program="$FUNCNAME";
    local version='$Revision$';
    local directory="false";
    local leading="false";
    local owner="";
    local group="";
    local mode="";
    local preserve="false";
    local -a files;

    args-loop "$@";

    local count="${#files[@]}";
    if [ $count -lt 1 ]; then
        if [ "$directory" == "true" ]; then
            abort "missing target directory";
        else
            abort "missing source file";
        fi;
    fi;

    if [ "$directory" == "true" ]; then
        install-dirs "${files[@]}";
    else
        if [ $count -lt 2 ]; then
            abort "missing destination";
        fi;

        local last="${files[$(($count-1))]}";
        if [ -d "$last" ]; then
            unset files[$(($count-1))];
            install-files "$last" "${files[@]}";
        elif [ $count -eq 2 ]; then
            install-file "${files[@]}";
        else
            local text1="installing multiple files, but last";
            local text2="argument, '$last' is not a directory";
            abort "$text1 $text2";
        fi;
    fi;
}

##############################################################################