aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/.drive-read-driverc
blob: e0969cfd9cabf7f4c9d7f2623dbcbde4ee68947b (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
#!/bin/bash
###################################################################################################
# .drive-read-driverc
###################################################################################################
# Copyright 2017-2023 Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License.
# You may not use this file except in compliance with the License. You may obtain a copy of the 
# License at:
#
#    https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# 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.
###################################################################################################

###################################################################################################
#
# Reading the right option from .driverc: the option listed below the [<command>] section
# supersedes the one listed below the [global] sections.
#
###################################################################################################
#
# Requirements:
# ------------
#
# - We assume that drive-google package is already installed.
#
# - This command must be called from within your local Google Drive folder(s).
#
# - <google_drive_folder>/.driverc:it must be properly setup, either manually or with drive-setup,
#                                  with the following settings:
#       * ...: cf. https://github.com/odeke-em/drive for a complete list of available  
#              options. They can only be set manually through .driverc or through drive-setup for
#              the sake of simplicity.
#              Cf. /etc/drive-google/.driverc for some examples.
#
###################################################################################################
#
# Parameters:
# ----------
#
# - <option>: what is its value?
#
# - <command>: related command corresponding to the section within .driveselect.
#              Cf. /etc/drive-google/.driveselect for a list of currently supported commands.
#
###################################################################################################
#
# Return codes:
# ------------
#
# - value of the <option> if it is defined
# - The value is null in the following cases:
#       + <google_drive_folder>/.driverc has not been found
#       + the <option> is not defined below at least one of the following sections (the first 3 are
#         equivalent):
#               * [global]
#               * [all]
#               * [*]
#               * [<command>]
#
###################################################################################################
#
# Usage examples:
# --------------
# cd ~/<google_drive_folder>/parent/folder/
# value=$(.drive-read-driverc depth push)
#
###################################################################################################
if [[ $LOG_DRIVE_READ_DRIVERC == 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

option=$1
command=$2

section='out'
command_section=false
value=0

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

if [[ ($return_code -ne 0) ]]; then
        # We cannot locate $gd_root_folder
        echo ""
        exit $return_code
fi
        
local_folder="$(pwd)"
local_folder_path_from_gd_root="${local_folder#$gd_root_folder}"

# If .driverc exists
if [[ -f "${gd_root_folder}"/.driverc ]]; then
        # Reading the $option within .driverc in the right [$command] or [global] section
        while IFS='' read -r line || [[ -n "$line" ]]; do
                # Checking for blank lines or comments
                if [[ (-z "${line}") || ("${line}" =~ ^#) ]]; then
                        continue
                # Checking for [global] or [all] or [*]
                elif [[ "$line" =~ (\[global\])|(\[all\])|(\[\*\]) ]]; then
                        section='in'
                # Checking for [$command] (or among other commands separated by /)
                elif [[ "$line" =~ \[.*${command}.*\] ]]; then
                        # [$command] section replaces [global] section
                        section='in'
                        command_section=true
                elif [[ ($section == 'in') && ("$line" =~ \[.*\]) ]]; then
                        if [[ $command_section == 'true' ]]; then
                                # We've just finished reading the last [$command] section: we're done
                                break
                        else
                                section='out'
                        fi
                elif [[ $section == 'in' ]]; then
                        option_tmp=$(echo "$line" | cut -d '=' -f 1)
                        if [[ "$option_tmp" == "$option" ]]; then
                                value=$(echo "$line" | cut -d '=' -f 2)
                                # Removing all spaces, tabs, line breaks
                                value=${value//[[:space:]]/}
                        fi
                fi
        done < "${gd_root_folder}/.driverc"

        # Returning the value
        echo "$value"
else
        # Nothing to return
        echo ""
fi