summaryrefslogtreecommitdiff
path: root/Library/move.sh
blob: 31c89e4c6e6ac91b136b9aae88696caf0d193f46 (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
#!/bin/bash

shopt -s extglob nullglob

function df_() {
    free=$(df -B1 "$1")
    free=${free% *%*}
    free=${free%%*( )}
    free=${free##* }
    echo "${free}"
}

function mv_() {
    src=$1
    dst=$2

    if [[ -e ${dst} ]]; then
        dst=$(mktemp -d /var/cydia.XXXXXX)
    else
        mkdir -p "${dst}"
    fi

    if [[ -e ${src} ]]; then
        chmod --reference="${src}" "${dst}"
        chown --reference="${src}" "${dst}"

        cp -aT "${src}" "${dst}" || {
            rm -rf "${dst}"
            exit 1
        }

        rm -rf "${src}"
    else
        chmod 775 "${dst}"
        chown root.admin "${dst}"
    fi

    ln -s "${dst}" "${src}"
}

function shift_() {
    dir=$1
    dst=$2

    if [[ -d ${dir} && ! -h ${dir} ]]; then
        used=$(du -bs "${dir}")
        used=${used%%$'\t'*}
        free=$(df_ /var)

        if [[ $((used + 524288)) -lt ${free} ]]; then
            mv_ "${dir}" "${dst}"
        fi
    elif [[ -h ${dir} && ! -e ${dir} ]]; then
        rm -f "${dir}"
        mv_ "${dir}" "${dst}"
    fi
}

shift_ "$@"