-
Notifications
You must be signed in to change notification settings - Fork 2
/
bmm-setup_enviroment
executable file
·156 lines (132 loc) · 6.13 KB
/
bmm-setup_enviroment
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
#!/bin/bash
## create new .env file or update(change) enviroment variables in .env file
# this script should be able to generate a new .env file in case one is not found
# it should also be able to edit an existing .env file with new settings easily
# should be noob-proof.
#TODO: add PATH automagically
# echo 'export PATH="$HOME/smm:$PATH"' >> $HOME/.bashrc && . $HOME/.bashrc
# Create or truncate the log file
LOG_FILE="setup_environment.log"
> "$LOG_FILE"
ENV_FILE="$HOME/bmm/.env" #work env
TEMPLATE="$HOME/bmm/.template_env" #fixed template
## edit or create new env file
if [ -f $ENV_FILE ]; then . $ENV_FILE;
else cp $TEMPLATE $ENV_FILE && . $ENV_FILE || {
echo -e "FATAL ERROR: Could not create env file.\nAborted."; exit 1;};
fi
# Keys must maintain same order as main_form() fields.
keys=(AUDIOROOT AUDIOBACKUP AUDIOJUNK VIDEOSJUNK SERIESROOT SERIESBACKUP \
MOVIESROOT MOVIESBACKUP CONCERTSROOT CONCERTSBACKUP STANDUPROOT \
STANDUPBACKUP AUDIO_OWNER SERIES_OWNER MOVIES_OWNER CONCERTS_OWNER \
STANDUP_OWNER GROUP)
#folders setup, user/groups setup
main_form () {
# open fd
exec 3>&1
# Store lines to $filled_form
filled_form=$(dialog --no-lines --ok-label "Update" --no-cancel --colors\
--backtitle "\Z7Bash Media Manager" \
--title "Setup Enviroment" \
--form " Fill in all fields or some features will not work.\n
Make sure paths start and end with '/'." \
25 80 0 \
"Audio root folder:" 1 2 "$AUDIOROOT" 1 27 46 0 \
"Audio backup folder:" 2 2 "$AUDIOBACKUP" 2 27 46 0 \
"Audio junk folder:" 3 2 "$AUDIOJUNK" 3 27 46 0 \
"Videos junk folder:" 4 2 "$VIDEOSJUNK" 4 27 46 0 \
"Series root folder:" 5 2 "$SERIESROOT" 5 27 46 0 \
"Series backup folder:" 6 2 "$SERIESBACKUP" 6 27 46 0 \
"Movies root folder:" 7 2 "$MOVIESROOT" 7 27 46 0 \
"Movies backup folder:" 8 2 "$MOVIESBACKUP" 8 27 46 0 \
"Concerts root folder:" 9 2 "$CONCERTSROOT" 9 27 46 0 \
"Concerts backup folder:" 10 2 "$CONCERTSBACKUP" 10 27 46 0 \
"Stand-up root folder:" 11 2 "$STANDUPROOT" 11 27 46 0 \
"Stand-up backup folder:" 12 2 "$STANDUPBACKUP" 12 27 46 0 \
"User owner of audios:" 13 2 "$AUDIO_OWNER" 13 27 46 0 \
"User owner of series:" 14 2 "$SERIES_OWNER" 14 27 46 0 \
"User owner of movies:" 15 2 "$MOVIES_OWNER" 15 27 46 0 \
"User owner of concerts:" 16 2 "$CONCERTS_OWNER" 16 27 46 0 \
"User owner of stand-up:" 17 2 "$STANDUP_OWNER" 17 27 46 0 \
"Group owner of media:" 18 2 "$GROUP" 18 27 46 0 \
2>&1 1>&3)
# if we cancel...
if [ $? -ne 0 ]; then
dialog --colors --no-lines \
--backtitle "\Z7Bash Media Manager" \
--title "Setup Enviroment" \
--infobox "Settings \Z1NOT\Zn updated!" 3 25
read
eval bmm-launcher; exit 1;
fi
# close fd
exec 3>&-
# Reads $filled_form and sed key#=line# to .env (ex GROUP="root")
i=0
while read -r line; do
# sed key-line
sed -i -e '/'"${keys[((i++))]}"'=/ s|=.*|='"\"${line}\""'|' $ENV_FILE
done <<< "${filled_form}"
#load newly entered form fields
. $ENV_FILE
# Error handling: add '/' to start/end of lines as needed
for ((i=0; i<${#keys[@]}; i++)); do
dir="${keys[i]}"
if [[ -z "${!dir}" ]]; then
log_entry="${dir} is empty. Skipping..."
else
formatted_dir="${!dir}"
if [[ $formatted_dir != /* ]]; then
formatted_dir="/$formatted_dir"
fi
if [[ $formatted_dir != */ ]]; then
formatted_dir="${formatted_dir}/"
fi
if [[ "${!dir}" != "${formatted_dir}" ]]; then
log_entry="${dir} was ${!dir}, corrected to ${formatted_dir}"
sed -i -e 's|^'"${dir}"'="'"${!dir}"'|'"${dir}"'="'"${formatted_dir}"'|' "$ENV_FILE"
. "$ENV_FILE"
else
log_entry="${dir} is already formatted correctly"
fi
fi
if ((i == 11)); then break; fi # Limit loop to the first 12 elements
# Append the log entry with date and time to the log file
formatted_date="$(date '+%Y-%m-%d %H:%M:%S')"
echo "$formatted_date: $log_entry" >> "$LOG_FILE"
done
dialog --colors --no-lines \
--backtitle "\Z7Bash Media Manager" \
--title "Setup Enviroment" \
--infobox "Settings updated." 3 21
read
# return to launcher
clear && eval bmm-launcher; exit 0;
}
## ARGUEMENTS
if [ "$1" = '-t' ] || [ "$1" = '--template' ]; then
#dont overwrite .env if it exists
if [ -f $ENV_FILE ]; then echo -e "$ENV_FILE exists!!!\nManually delete it if you want to reset to a template."; exit 1; fi
echo -e "Creating new env file from template at $ENV_FILE"
cp $TEMPLATE $ENV_FILE || { echo -e "Could not create $ENV_FILE.\Maybe check permissions...\nAborted."; exit 1; }
elif [ "$1" = '-p' ] || [ "$1" = '--path' ]; then
echo 'export PATH="$HOME/bmm:$PATH"' >> $HOME/.bashrc && . $HOME/.bashrc
echo 'You may need to reload bashrc (. .bashrc) or log out and in again.'
elif [ "$1" = '-c' ] || [ "$1" = '--check' ]; then
for dir in "${keys[@]:0:12}"
do
dir_check "${!dir}"
done
elif [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
if ! command -v $BANNER >> /dev/null; then echo -e "${WARN}BASH MEDIA MANAGER${NC}"; else eval $BANNER; fi
echo -e "${HEAD}Setup Enviroment"
echo -e "${OK}Create new or update enviroment variables in ${WARN}.env${OK} file.\n${NC}"
echo -e "Use with no arguements to enter the GUI or\nedit ${WARN}~/bmm/.env${NC} to configure your folders."
echo -e "Usage: bmm-setup_enviroment [-arg] [--arg]\n"
echo -e " -p [--path] -> Adds software root to PATH."
echo -e " -t [--template] -> Creates new env file from template."
echo -e " -c [--check] -> Check if media folders exist or create them."
echo -e " -h [--help] -> Shows this screen."
else
main_form;
fi