summarylogtreecommitdiffstats
path: root/rc5-mount-remote-google.sh
blob: 7457bae4ca7402858aa05f648e4867b27c896e4b (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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
set -u
set -e


XUSERS=($(who|grep -E "\(:[0-9](\.[0-9])*\)"|awk '{print $1$NF}'|sort -u))
for XUSER in "${XUSERS[@]}";
do
	echo $XUSER;
	NAME=(${XUSER/(/ })
	DISPLAY=${NAME[1]/)/}
	DBUS_ADDRESS=unix:path=/run/user/$(id -u ${NAME[0]})/bus

done

user_target_link_count=0
user_target_bind_count=0
user_target_bind_mount_count=0
user_target_mount_count=0
user_target_chown_count=0


toHelp() {
    echo "hello, world"
}

# proxy list
proxy_off(){
     unset http_proxy
     unset https_proxy
     unset socks5_proxy
     echo -e "已关闭代理"
}
proxy_on() {
     export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
     export http_proxy=http://127.0.0.1:8118
     export https_proxy=$http_proxy
     export socks5_proxy="127.0.0.1:1080"  
     echo -e "已开启代理"
}

rollback() {
    echo "umount user context"
    proxy_off
    sudo -u ${NAME[0]} DISPLAY=${DISPLAY} \
                DBUS_SESSION_BUS_ADDRESS=${DBUS_ADDRESS} \
                PATH=${PATH} \
                notify-send "rclone" "shell exec error"
 
    exit
}


trap rollback INT TERM EXIT


toRunGoogleContainer() {

    # unit bind target exist dirs
    echo "check bind target exist dirs"
    if ! test -d "$2" 
    then
        mkdir -p "$2"
        user_target_bind_count=`expr ${user_target_bind_count} + 1`   
        
    fi
    echo "check bind target not exist by of create $user_target_bind_count dirs"

    #check target dirs is mount bind
    echo "check target dirs is mount bind"
    if grep -qs "$2" /proc/mounts ;
    then
        user_target_bind_mount_count=`expr ${user_target_bind_mount_count} + 1`    
    fi
    echo "check target dirs is mount bind of $user_target_bind_mount_count"
    #bind target user context 
    if [ $user_target_bind_mount_count -gt 0 ] 
    then

        echo "exist ${user_target_bind_mount_count} bind dir of target,exit!"
        exit 1
    else
        #run load mounts target 
        if test -d "$2" 
        then
            echo "$2"     
            sudo -u ${NAME[0]} DISPLAY=${DISPLAY} \
                DBUS_SESSION_BUS_ADDRESS=${DBUS_ADDRESS} \
                PATH=${PATH} \
                notify-send "rclone" "enable google"
            if [ `whoami` == "root" ];then
                sudo -H -E  -u dmeck bash -c "$(declare -f proxy_on); proxy_on &&  nohup rclone mount google:  \"$2\" --allow-other --allow-non-empty --vfs-cache-mode writes  &"
            else
               proxy_on &&  nohup rclone mount google:  "$2" --allow-other --allow-non-empty --vfs-cache-mode writes   &
            fi 
             
            user_target_mount_count=`expr ${user_target_mount_count} + 1`   
        
        fi
        echo "target dirs is mount bind of $user_target_mount_count"
        

        
    fi
}

toRun() {


    user_target_bind="/media/target/home/dmeck/work/google"

    if [ $# -eq 1 ]; then
        case $1 in
        google)
            # if you need to launch another app, add the app name here.
            echo " start $1 application"
            toRunGoogleContainer $1 $user_target_bind
            ;;
        all)
            echo "start all application"
            toRunGoogleContainer $1 $user_target_bind
            ;;
        *)
            echo "unsupport your application"
            ;;
        esac
    else
        toHelp
    fi
}

toStopGoogleContaine() {

    proxy_off

    sudo -u ${NAME[0]} DISPLAY=${DISPLAY} \
        DBUS_SESSION_BUS_ADDRESS=${DBUS_ADDRESS} \
        PATH=${PATH} \
        notify-send "rclone" "disable google"
    echo ${DBUS_ADDRESS}
	fusermount -uz  "$2"
}

toStop() {
    user_target_bind="/media/target/home/dmeck/work/google"

    if [ $# -eq 1 ]; then
        case $1 in
        google)
            # if you need to stop another app, add the app name here.
            echo "stop $1 application"
            toStopGoogleContaine $1 $user_target_bind
            ;;
        all)
            echo "stop all application"
            toStopGoogleContaine $1 $user_target_bind
            ;;
        *)
            echo "unsupport your application"
            ;;
        esac
    else
        toHelp
    fi
}


case $1 in
-r)
    toRun $2 
    ;;
-s)
    toStop $2
    ;;
-h) ;&
*)
    toHelp
    ;;
esac


trap - INT TERM EXIT