/
mega_mkdir.sh
executable file
·217 lines (206 loc) · 7.91 KB
/
mega_mkdir.sh
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
#!/bin/bash
#
# The content of this file are licensed under the MIT License (https://opensource.org/licenses/MIT)
# MIT License
#
# Copyright (c) 2018 Paul Moss
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Script to check and see if a file or folder exist on a mega.nz account
# Created by Paul Moss
# Created: 2018-05-27
# File Name: mega_mkdir.sh
# Github: https://github.com/Amourspirit/mega_scripts
# Help: https://amourspirit.github.io/mega_scripts/mega_mkdirsh.html
#
# This script will make directories if they do not exist for a path.
# This is the mega.nz equivalent of mkdir -p for UNIX
# Eg: ./mega_mkdir.sh -p "/testdir/2018/bigtest/deep/deeper/bottom"
#
# -p: Optional: -p pass in the Path to create if it does not exist on Mega.nz
# Example: /bin/bash /usr/local/bin/mega_mkdir.sh -p '/testdir/2018/bigtest/deep/deeper/bottom'; echo $?
# -i: Optional: -i pass in the configuration file that contains the account information for mega.nz. Defaults to ~/.megarc
# -v: Display the current version of this script
# -h: Display script help
#
# Exit Codes
# Code Defination
# 0 Normal Exit. No Errors Encountered.
# 40 If no argument is give for parameter one, the path to check.
# 41 If megamkdir had error creating directory
# 42 If a file was found with the same name as part of the path
# 102 megals not found. Megtools requires installing
# 111 Optional argument Param 2 was passed in but the config can not be found or we do not have read permissions
# 115 megamkdir not found. Megtools requires installing
# trims white space from input
MS_VERSION='1.3.5.0'
MEGA_SCRIPTS_CONFIG="'${HOME}/.mega_scriptsrc'"
function trim () {
local var=$1;
var="${var#"${var%%[![:space:]]*}"}"; # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}"; # remove trailing whitespace characters
echo -n "$var";
}
# create an array that contains configuration values
# put values that need to be evaluated using eval in single quotes
typeset -A SCRIPT_CONF # init array
SCRIPT_CONF=( # set default values in config array
[MEGA_EXIST_FILE_NAME]="mega_dir_file_exist.sh"
[MT_MEGA_MKDIR]='megamkdir'
)
if [[ -f "${MEGA_SCRIPTS_CONFIG}" ]]; then
# make tmp file to hold section of config.ini style section in
TMP_CONFIG_COMMON_FILE=$(mktemp)
# SECTION_NAME is a var to hold which section of config you want to read
SECTION_NAME="MEGA_COMMON"
# sed in this case takes the value of SECTION_NAME and reads the setion from ~/config.ini
sed -n '0,/'"$SECTION_NAME"'/d;/\[/,$d;/^$/d;p' "" > $TMP_CONFIG_COMMON_FILE
# test tmp file to to see if it is greater then 0 in size
test -s "${TMP_CONFIG_COMMON_FILE}"
if [ $? -eq 0 ]; then
# read the input of the tmp config file line by line
while read line; do
if [[ "$line" =~ ^[^#]*= ]]; then
setting_name=$(trim "${line%%=*}");
setting_value=$(trim "${line#*=}");
SCRIPT_CONF[$setting_name]=$setting_value
fi
done < "$TMP_CONFIG_COMMON_FILE"
fi
# release the tmp file that is contains the current section values
unlink $TMP_CONFIG_COMMON_FILE
fi
BASH="$(command -v bash)"
#SCRIPT_DIR=$(dirname "$0")
# See Also: https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
MEGA_DEFAULT_ROOT="/Root"
MEGA_EXIST_FILE_NAME=${SCRIPT_CONF[MEGA_EXIST_FILE_NAME]}
MEGA_EXIST_FILE_SCRIPT=$SCRIPT_DIR"/"$MEGA_EXIST_FILE_NAME
CURRENT_CONFIG=""
HAS_CONFIG=0
DIR=""
NEW_DIR=""
EXIT_CODE=0
MT_MEGA_MKDIR=${SCRIPT_CONF[MT_MEGA_MKDIR]}
MT_MEGA_MKDIR=$(eval echo ${MT_MEGA_MKDIR})
# test to see if megamkdir is installed or exsit the location set in the .mega_scriptsrc file
if ! [[ -x "$(command -v ${MT_MEGA_MKDIR})" ]]; then
exit 115
fi
usage() { echo "$(basename $0) usage:" && grep "[[:space:]].)\ #" $0 | sed 's/#//' | sed -r 's/([a-z])\)/-\1/'; exit 0; }
[ $# -eq 0 ] && usage
while getopts ":hvp:i:" arg; do
case $arg in
p) # Required: Specify -p the Path to create if it does not exist on Mega.nz Example: /bin/bash /usr/local/bin/mega_mkdir.sh -p '/testdir/2018/bigtest/deep/deeper/bottom'; echo $?
DIR="${OPTARG}"
;;
i) # Optional: Specify -i the configuration file to use that contain the credentials for the Mega.nz account you want to access.
CURRENT_CONFIG="${OPTARG}"
;;
v) # -v Display version info
echo "$(basename $0) version:${MS_VERSION}"
exit 0
;;
h | *) # -h Display help.
echo 'For online help visit: https://amourspirit.github.io/mega_scripts/mega_mkdirsh.html'
usage
exit 0
;;
esac
done
# the follow vars are eval in case they contain other expandable vars such as $HOME or ${USER}
DIR=$(eval echo ${DIR})
CURRENT_CONFIG=$(eval echo ${CURRENT_CONFIG})
if [ -z "$DIR" ]; then
# No argument supplied for path! Exiting
exit 40
fi
if [[ -n "$CURRENT_CONFIG" ]]; then
# Argument is given for default configuration for that contains user account and password
test -r "${CURRENT_CONFIG}"
if [ $? -ne 0 ]; then
#Config file does not exist or can not gain read access! Exit Code: 111
exit 111
fi
HAS_CONFIG=1
fi
if [[ HAS_CONFIG -eq 1 ]]; then
${BASH} "${MEGA_EXIST_FILE_SCRIPT}" -p "${DIR}" -i "${CURRENT_CONFIG}"
else
${BASH} "${MEGA_EXIST_FILE_SCRIPT}" -p "${DIR}"
fi
if [[ $? -eq 2 ]]; then
# Directory already exist
exit 0
fi
# if result is 0 we will continue
case $? in
1 )
# directory found root
exit 0
;;
2 )
# directory found
exit 0
;;
3 )
# file found
exit 42
;;
102 )
#megals not found. Megtools requires installing
exit 102
;;
esac
IFS='/' read -ra PARTS <<< "$DIR"
for i in "${PARTS[@]}"; do
# process "$i"
if [[ -n $i ]]; then
NEW_DIR="${NEW_DIR}/$i"
if [[ HAS_CONFIG -eq 1 ]]; then
${BASH} "${MEGA_EXIST_FILE_SCRIPT}" -p "${NEW_DIR}" -i "${CURRENT_CONFIG}"
else
${BASH} "${MEGA_EXIST_FILE_SCRIPT}" -p "${NEW_DIR}"
fi
EXIST_RESULT=$?
if [[ $EXIST_RESULT -eq 0 ]]; then
if [[ HAS_CONFIG -eq 1 ]]; then
${MT_MEGA_MKDIR} --config "${CURRENT_CONFIG}" "${MEGA_DEFAULT_ROOT}/${NEW_DIR}"
else
${MT_MEGA_MKDIR} "${MEGA_DEFAULT_ROOT}/${NEW_DIR}"
fi
EXIT_CODE=$?
if [[ EXIT_CODE -ne 0 ]]; then
#echo "Error Creating directory '$NEW_DIR' megamkdir Error Code: $EXIT_CODE"
EXIT_CODE=41
break
fi
else
if [[ $EXIST_RESULT -ne 2 ]]; then
# unable to continue. this part of the path is not a directory and we cannot continue.
# most likely a file
EXIT_CODE=42
break
fi
fi
fi
done
exit $EXIT_CODE