]> git.saurik.com Git - cydia.git/blob - Library/move.sh
c0fc10cda47dad23990b8c57c6d5c19907156a13
[cydia.git] / Library / move.sh
1 #!/bin/bash
2
3 shopt -s extglob nullglob
4
5 if [[ ${1:0:1} == - ]]; then
6 v=$1
7 shift 1
8 else
9 v=
10 fi
11
12 function df_() {
13 free=$(df -B1 "$1")
14 free=${free% *%*}
15 free=${free%%*( )}
16 free=${free##* }
17 echo "${free}"
18 }
19
20 function mv_() {
21 src=$1
22
23 mkdir -p /var/stash
24 tmp=$(mktemp -d /var/stash/_.XXXXXX)
25 dst=${tmp}/${src##*/}
26
27 mkdir -- "${dst}" || {
28 rmdir -- "${tmp}"
29 exit 1
30 }
31
32 if [[ -e ${src} ]]; then
33 chmod --reference="${src}" "${dst}"
34 chown --reference="${src}" "${dst}"
35
36 cp -aT $v "${src}" "${dst}" || {
37 rm -rf "${tmp}"
38 exit 1
39 }
40
41 rm -rf $v "${src}"
42 else
43 chmod 775 "${dst}"
44 chown root.admin "${dst}"
45 fi
46
47 ln -s "${dst}" "${src}"
48 echo -n "${src}" >"${tmp}.lnk"
49 }
50
51 function shift_() {
52 dir=${1%/}
53
54 if [[ -d ${dir} && ! -h ${dir} ]]; then
55 used=$(/usr/libexec/cydia/du -bs "${dir}")
56 used=${used%%$'\t'*}
57 free=$(df_ /var)
58
59 if [[ $((used + 524288)) -lt ${free} ]]; then
60 mv_ "${dir}"
61 fi
62 elif [[ ! -e ${dir} ]]; then
63 rm -f "${dir}"
64 mv_ "${dir}"
65 fi
66 }
67
68 shift_ "$@"