aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/drive-setup
blob: aa5247a198d0c62ab044da9f26c20113365a2666 (about) (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
###################################################################################################
# drive-setup
###################################################################################################
# Copyright 2017-2022 Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 
# in compliance with the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# This script is using a project hosted at https://github.com/odeke-em/drive, originally developed 
# by Burcu Dogan while working on the Google Drive team and now maintained by Emmanuel T Odeke.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if 
#               changes were made. You may do so in any reasonable manner, but not in any way that 
#               suggests the licensor endorses you or your use.
###################################################################################################

###################################################################################################
#
# Setting up drive by:
#       + configuring general settings:                 <google_drive_folder>/.driverc
#       + selecting objects using regular expressions:  <google_drive_folder>/.driveignore
#       + selecting objects using standard wildcards:   ./.driveselect
#
# Those settings affect all drive-<command> or a specific <command>.
# The setup can be done manually by editing the corresponding configuration file or with the help
# of GUI windows.
#
###################################################################################################
#
# Requirements:
# ------------
#
# - We assume that:
#       + xdg-utils & zenity package are already installed.
#
# - This command must be called from within your local Google Drive folder(s).
#
###################################################################################################
#
# Parameters:
# ----------
#
# - <anything> will display this help message
#
###################################################################################################
#
# Usage example:
# -------------
#
# cd ~/<google_drive_folder>/any/sub/folder
# drive-setup
#
###################################################################################################
if [[ $LOG_DRIVE_SETUP == ON ]]; then
        # Getting the last existing file descriptor of ~/.drive-google/logs/drive-google.log
        fd=$(ls -al /proc/$$/fd | grep ~/.drive-google/logs/drive-google.log | sed -E 's|^.* ([0-9]+) -> .*$|\1|g' | sort -V | tail -n 1)

        # Case where fd is unset, i.e the file ~/.drive-google/logs/drive-google.log has not yet been opened
        if [[ -z $fd ]]; then
                lowest_unused_fd () 
                {
                        local fd=0
                        while [ -e /proc/$$/fd/${fd} ]; do
                                fd=$((fd+1))
                        done
                        echo $fd
                }
                fd=$(lowest_unused_fd)

                # Opening ~/.drive-google/logs/drive-google.log as file descriptor $fd for appending
                eval "exec $fd>> ~/.drive-google/logs/drive-google.log"
        fi

        # Writing the trace output generated when set -x is enabled to file descriptor $fd 
        eval "BASH_XTRACEFD=$fd"
        # Logging line numbers - We could also use ${0} for ${BASH_SOURCE}
        BASH_SOURCE_BASENAME=$(basename ${BASH_SOURCE})
        export PS4='${BASH_SOURCE_BASENAME}.${LINENO}+ '
        # Expanding all variables and prints the full commands before output of the command
        set -x
fi

# Preventing globbing (pathname expansion)
set -o noglob
# Allowing extended globbing
shopt -s extglob
# Expanding aliases
shopt -s expand_aliases

# Including alias definitions to sed/grep/awk/perl commands for readability sake
. /usr/include/drive-google/aliases

# Checking for no parameter
if [[ $# -eq 0 ]]; then
        local_folder="$(pwd)"

        # Removing all occurrences of '&' in $object as a workaround to a markup ampersand issue with zenity
        noamp_local_folder="$(echo "${local_folder}" | rm_ampersand)"

        # Finding local root Google Drive folder
        gd_root_folder="$(.drive-find-gd)"
        return_code=$?

        # Removing all occurrences of '&' in $gd_root_folder as a workaround to a markup ampersand issue with zenity
        noamp_gd_root_folder="$(echo "${gd_root_folder}" | rm_ampersand)"

        case $return_code in
                0)
                        # Everything is fine - we continue
                        ;;
                7)
                        zenity --warning --title "Drive context found in $noamp_gd_root_folder" --text="We have moved to $noamp_gd_root_folder\n\nIf you prefer to select another Google Drive folder:\n\t--> 'Change working directory'" --width 1400

                        cd "$gd_root_folder"

                        local_folder="$(pwd)"

                        # Removing all occurrences of '&' in $object as a workaround to a markup ampersand issue with zenity
                        noamp_local_folder="$(echo "${local_folder}" | rm_ampersand)"
                        ;;
                8)
                        zenity --error --title "No drive context found below and above $noamp_local_folder" --text="- either drive has not been initialized properly (at least one hop away from '/') with:\n\t--> '# drive init google_drive_folder' within a terminal\n- or we are not located below your google_drive_folder top level branch:\n\t--> 'Change working directory'" --width 1400
                        ;;
                *)
                        # Something is fatally wrong
                        zenity --error --title "Fatal error in drive-setup" --text="Unknown return code from .drive-find-gd: $return_code\n\nPlease report that issue to:\nhttps://gitlab.com/jean-christophe-manciot/Drive/issues" --width 1200
                        exit 99
                        ;;
        esac

        local_folder_path_from_gd_root=${local_folder#$gd_root_folder}

        while :
        do
                operation_type=$(zenity --list  \
                                        --radiolist \
                                        --width=750 \
                                        --height=220 \
                                        --title "Selecting objects & configuring general settings" \
                                        --column "Pick" \
                                        --column "Configuration Type" FALSE "Manual" TRUE "Automatic" FALSE Exit)
                case $operation_type in
                        Manual)
                                select_command=$(zenity --list  \
                                                        --radiolist \
                                                        --width=1500 \
                                                        --height=280 \
                                                        --title "Selecting objects & configuring general settings" \
                                                        --column "Pick" \
                                                        --column "Settings" FALSE "Configure default editor" FALSE "Exclude/Include objects using regular expressions in $gd_root_folder/.driveignore" FALSE "Include/Exclude objects using standard wildcards  in $local_folder/.driveselect" TRUE "Setup Drive by editing its general settings       in $gd_root_folder/.driverc" FALSE Exit)
                                case $select_command in
                                        "Configure default editor")
                                                while :
                                                do
                                                        editor=$(zenity --list  \
                                                                        --radiolist \
                                                                        --width=750 \
                                                                        --height=300 \
                                                                        --title "Configuring default editor" \
                                                                        --column "Pick" \
                                                                        --column "Editor" FALSE "Emacs GUI" FALSE "Emacs Terminal" TRUE "Gedit" FALSE "Kate" FALSE "Vim" FALSE Exit)
                                                        file_type="$(xdg-mime query filetype /etc/drive-google/.driverc)"
                                                        case $editor in
                                                                "Emacs GUI")
                                                                        xdg-mime default emacs24.desktop $file_type
                                                                        return_code=$?
                                                                        if [[ $return_code -eq 0 ]]; then
                                                                                zenity --info --title "Configuring default editor" --text="$editor has been successfully set as the default editor for the following file type:\n\t--> $file_type (.drive*)" --width 800
                                                                        else
                                                                                zenity --error --title "Configuring default editor" --text="$editor has **not** been set as default editor\n\nThe error code is $return_code" --width 800
                                                                        fi
                                                                        ;;
                                                                "Emacs Terminal")
                                                                        xdg-mime default emacs24-term.desktop $file_type
                                                                        return_code=$?
                                                                        if [[ $return_code -eq 0 ]]; then
                                                                                zenity --info --title "Configuring default editor" --text="$editor has been successfully set as the default editor for the following file type:\n\t--> $file_type (.drive*)" --width 800
                                                                        else
                                                                                zenity --error --title "Configuring default editor" --text="$editor has **not** been set as default editor\n\nThe error code is $return_code" --width 800
                                                                        fi
                                                                        ;;
                                                                "Gedit")
                                                                        xdg-mime default gedit.desktop $file_type
                                                                        return_code=$?
                                                                        if [[ $return_code -eq 0 ]]; then
                                                                                zenity --info --title "Configuring default editor" --text="$editor has been successfully set as the default editor for the following file type:\n\t--> $file_type (.drive*)" --width 800
                                                                        else
                                                                                zenity --error --title "Configuring default editor" --text="$editor has **not** been set as default editor\n\nThe error code is $return_code" --width 800
                                                                        fi
                                                                        ;;
                                                                "Kate")
                                                                        xdg-mime default org.kde.kate.desktop $file_type
                                                                        return_code=$?
                                                                        if [[ $return_code -eq 0 ]]; then
                                                                                zenity --info --title "Configuring default editor" --text="$editor has been successfully set as the default editor for the following file type:\n\t--> $file_type (.drive*)" --width 800
                                                                        else
                                                                                zenity --error --title "Configuring default editor" --text="$editor has **not** been set as default editor\n\nThe error code is $return_code" --width 800
                                                                        fi
                                                                        ;;
                                                                "Vim")
                                                                        xdg-mime default vim.desktop $file_type
                                                                        return_code=$?
                                                                        if [[ $return_code -eq 0 ]]; then
                                                                                zenity --info --title "Configuring default editor" --text="$editor has been successfully set as the default editor for the following file type:\n\t--> $file_type (.drive*)" --width 800
                                                                        else
                                                                                zenity --error --title "Configuring default editor" --text="$editor has **not** been set as default editor\n\nThe error code is $return_code" --width 800
                                                                        fi
                                                                        ;;
                                                                *)
                                                                        break
                                                                        ;;
                                                        esac
                                                done
                                                ;;
                                        "Exclude/Include objects using regular expressions in $gd_root_folder/.driveignore")
                                                zenity --warning --title "Exclude some objects using regular expressions in $noamp_gd_root_folder/.driveignore" --text="By default, all your objects recursively found below the current working directory are affected by the chosen drive command.\n\nIf you are not familiar with regular expressions, you should rather use Include/Exclude command in previous menu\n\nSave $noamp_gd_root_folder/.driveignore when you re done modifying it" --width=1400
                                
                                                xdg-open "${gd_root_folder}/.driveignore"
                                                ;;
                                        "Include/Exclude objects using standard wildcards  in $local_folder/.driveselect")
                                                zenity --warning --title "Include/Exclude a list of objects affecting each drive command in current $noamp_local_folder/.driveselect" --text="By default, all your objects recursively found below the current working directory are affected by the chosen drive command.\n\nAll paths are expected to originate from current working directory: ${noamp_local_folder}/\n\nSave $noamp_local_folder/.driveselect when you're done modifying it" --width=1400
                                
                                                if [[ ! -f ./.driveselect ]]; then
                                                        cp /etc/drive-google/.driveselect .
                                                fi
                                                xdg-open ".driveselect"
                                                ;;
                                        "Setup Drive by editing its general settings       in $gd_root_folder/.driverc")
                                                zenity --warning --title "Control Drive by editing its general settings in $noamp_gd_root_folder/.driverc" --text="By default, all your objects recursively found below the current working directory are affected by the chosen drive command.\n\nSave $noamp_gd_root_folder/.driverc when you're done modifying it" --width=1400
                                
                                                xdg-open "${gd_root_folder}/.driverc"
                                                ;;
                                esac
                                ;;
                        Automatic)
                                ;;
                        *)
                                break
                                ;;
                esac
        done

        exit 0
else
        # Any parameter
        end_of_help_message_line_number=$(awk '/^if \[\[ \$LOG_/{ print NR; exit }' $(which drive-setup))
        ((end_of_help_message_line_number--))
        awk -v var=$end_of_help_message_line_number 'NR >= 20 && NR <= var' $(which drive-setup) | zenity --text-info --title "drive-setup help" --width 1150 --height 1000
        exit 1
fi