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

shopt -s extglob nullglob

if [[ ${1:0:1} == - ]]; then
    v=$1
    shift 1
else
    v=
fi

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

function mv_() {
    src=$1

    if [[ ! -e /var/stash ]]; then
        mkdir -p /var/db/stash
        /usr/libexec/cydia/setnsfpn /var/db/stash
        ln -s -t /var /var/db/stash
    elif [[ -d /var/stash ]]; then
        /usr/libexec/cydia/setnsfpn /var/stash
    fi

    tmp=$(mktemp -d /var/stash/_.XXXXXX)
    dst=${tmp}/${src##*/}

    chmod 755 "${tmp}"
    chown root.admin "${tmp}"

    mkdir -- "${dst}" || {
        rmdir -- "${tmp}"
        exit 1
    }

    echo -n "${src}" >"${tmp}.lnk"

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

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

        mv $v "${src}" "${src}.moved"
        ln -s "${dst}" "${src}"
        rm -rf $v "${src}.moved"
    else
        chmod 775 "${dst}"
        chown root.admin "${dst}"
        ln -s "${dst}" "${src}"
    fi
}

function shift_() {
    dir=${1%/}

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

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

CFVERSION=$(cfversion)
if [[ ${CFVERSION%.*} -lt 1349 || "$(uname -p)" == "arm" && ${CFVERSION%.*} -lt 1440 ]]; then
    shift_ "$@"
fi