-
Notifications
You must be signed in to change notification settings - Fork 0
/
yacm
executable file
·801 lines (698 loc) · 23.9 KB
/
yacm
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#!/bin/bash
set -e
YACM_VERSION="0.0.1";
SKIP_CONFIRMATION=;
YACM_BASE_DIR=;
YACM_DIR=;
YACM_PROFILES_DIR=;
YACM_SCRIPTS_DIR=;
# SUDO is used to determine if yacm is being run by root or not
SUDO='';
if [[ $EUID != 0 ]]; then
SUDO='sudo';
fi
function main() {
_tab=$'\t'
for param in "$@"; do
param="${param//\\/\\\\}"
param="${param//$_tab/\\$_tab}"
param="${param// /\\ }"
_fc+=( "$param" )
done
# Create YACM_DIR, YACM_BASE_DIR, YACM_PROFILES_DIR, and YACM_SCRIPTS_DIR
# if they doesn't exist yet
[ -d "$YACM_BASE_DIR" ] || mkdir -p "$YACM_BASE_DIR"
[ -d "$YACM_DIR" ] || mkdir -p "$YACM_DIR"
[ -d "$YACM_PROFILES_DIR" ] || mkdir -p "$YACM_PROFILES_DIR"
[ -d "$YACM_SCRIPTS_DIR" ] || mkdir -p "$YACM_SCRIPTS_DIR"
internal_commands="^(bootstrap|help|-h|--help|init|profile|version|-v|--version)$"
if [ -z "$1" ] ; then
# No argumnts will result in help()
help;
elif [[ "$1" =~ $internal_commands ]]; then
case "$1" in
bootstrap)
bootstrap "$2" "$3";;
help|-h|--help)
help;;
init)
init "$2";;
profile)
profile "$2" "$3" ;;
version|-v|--version)
version;;
esac
else
# Invalid command, throw error and run help menu
echo -e '\e[31mIt appears you have entered an invalid command!\e[0m\n';
help
fi
}
function help() {
cat << EOF
Configure your system(s) with any executable(s) YOU design!
Usage: yacm [global options] <command> [subcommand]
Commands:
yacm bootstrap - Bootstrap the current system
yacm init - Initalize a new profile for a system
yacm profile - Creates and manages system profiles
yacm help - Prints this menu
yacm version - Prints current yacm version
Alternative commands:
--help - Prints this menu
--version - Prints current yacm version
-h - Prints this menu
-v - Prints current yacm version
Global subcommands:
help - Prints help menu for subcommand
Global options (use these before the command, if any):
--yacm-base-dir - Overrides the standard yacm base dir
default: {$HOME/.config}
--yacm-dir - Overrides the standard yacm dir
default: {$YACM_BASE_DIR/yacm}
--yacm-profiles-dir - Overrides the standard yacm profiles dir
default: {$YACM_DIR/profiles}
--yacm-scripts-dir - Overrides the standard yacm scripts dir
default: {$YACM_DIR/scripts}
EOF
}
function bootstrap_help() {
cat << EOF
Used to bootstrap the current system with the currently
set profile or with the profile specified.
Usage: yacm bootstrap [subcommand]
Subcommands:
help - Prints this menu
profile - Used to specify which profile to use
Alternative subcommands:
--help - Prints this menu
-h - Prints this menu
EOF
}
function init_help() {
cat << EOF
Used to create profile if none are present and
set default profile for current system.
Usage: yacm init [subcommand]
Subcommands:
help - Prints this menu
Alternative subcommands:
--help - Prints this menu
-h - Prints this menu
EOF
}
function profile_help() {
cat << EOF
Used to create and manage yacm profiles
Usage: yacm profile <subcommand> [profile name]
Subcommands:
create - Creates a new profile
default - Sets default profile for current system
delete - Deletes a profile
list - List profiles on system
help - Prints this menu
Alternative subcommands:
--help - Prints this menu
--rm - Deletes a profile
-h - Prints this menu
-l - List profiles on system
EOF
}
function print_default_warning() {
echo -e "\nDefault profile set! It is highly recommended to add:";
echo -e "\e[32m$YACM_DIR/default_profile\e[0m to your \e[32m.gitignore\e[0m";
echo "if you keep your yacm directory in source control to allow";
echo "you to set a default profile on all of your systems.";
}
function bootstrap() {
local default_profile;
local subcommand=$1;
if [[ -z $subcommand ]]; then
echo "Checking current default profile...";
if [[ -f $YACM_DIR/default_profile ]]; then
default_profile="$(cat "$YACM_DIR"/default_profile 2>/dev/null)";
if [[ -f $YACM_PROFILES_DIR/$default_profile.yml ]]; then
bootstrap_prompt "$default_profile";
else
echo -e "\n\e[31mSpecified default profile, \e[32m$default_profile\e[31m, doesn't exist!.\e[0m";
echo -e "Please run: \e[32myacm profile default\e[0m to set default for this system or";
echo -e "run: \e[32myacm bootstrap profile [profile_name]\e[0m to specify which profile to use.";
fi
else
echo -e "\n\e[31mNo default profile currently set!.\e[0m";
echo -e "Please run: \e[32myacm profile default\e[0m to set default for this system or";
echo -e "run: \e[32myacm bootstrap profile [profile_name]\e[0m to specify which profile to use.";
fi
else
case "$subcommand" in
help|-h|--help)
bootstrap_help;;
profile)
if [[ -f $YACM_PROFILES_DIR/$2.yml ]]; then
bootstrap_prompt "$2";
else
echo -e "\e[31mERROR: provided profile doesn't exist!.\e[0m";
echo "Please specify a profile that exists.";
echo -e "To see profiles that exists run: \e[32m yacm profile list\e[0m.";
fi;;
*)
echo -e "\e[31mUnrecognized subcommand for bootstrap command.\e[0m\n";
bootstrap_help;;
esac
fi
}
function init() {
local subcommand=$1;
if [[ -n $subcommand ]]; then
case "$subcommand" in
help|-h|--help)
init_help;;
*)
echo -e "\e[31mUnrecognized subcommand for init command.\e[0m\n";
init_help;;
esac
else
echo -e "Initializng yacm...";
if [ -n "$(ls "$YACM_PROFILES_DIR"/*.yml 2>/dev/null)" ]; then
if [ ! -f "$YACM_DIR"/default_profile ]; then
echo "";
profile default;
else
local current_default;
echo -e "\nThe current default is set to:";
current_default=$(head -n 1 "$YACM_DIR"/default_profile);
echo -e " \e[32m$current_default\e[0m";
read -rp "Would you like to change the defaul profile? (y/N)" yn;
case $yn in
[Yy]*)
echo "";
profile default;;
[Nn]*)
echo "Keeping current default profile...";;
"")
echo "Keeping current default profile...";;
*)
echo -e "\e[31mPlease answer yes or no.\e[0m";;
esac
fi
else
profile create;
fi
echo -e "\nFinished initializing yacm!";
fi
}
function profile() {
local subcommand=$1;
if [[ -n $subcommand ]]; then
case "$subcommand" in
help|-h|--help)
profile_help;;
list|-l)
if [ -n "$(ls "$YACM_PROFILES_DIR"/*.yml 2>/dev/null)" ]; then
local current_default;
echo "Profiles:";
for file in "$YACM_PROFILES_DIR"/*.yml; do
echo " $(basename "$file" .yml)";
done
echo -e "\nThe current default is set to:";
if [ -f "$YACM_DIR"/default_profile ]; then
current_default=$(head -n 1 "$YACM_DIR"/default_profile);
echo -e " \e[32m$current_default\e[0m\n";
else
echo -e " \e[31mNo default profile currently set\e[0m";
fi
else
echo -e "\e[31mNo profiles present!\e[0m\n";
echo -e "\e[31mPlease run \e[32myacm init\e[31m!\e[0m";
fi;;
create|--create)
echo -e "Creating new profile...";
if [ -z "$2" ]; then
echo "";
read -rp "What would you like this profile to be called?: " profile_name;
else
profile_name=$2;
fi
if [[ ! -f "$YACM_PROFILES_DIR/$profile_name.yml" ]]; then
profile_create "$profile_name";
else
echo "Profile already exists!";
read -rp "Would you like to recreate it? (y/N)" yn;
case $yn in
[Yy]*)
rm "$YACM_PROFILES_DIR"/"$profile_name".yml;
profile_create "$profile_name";;
[Nn]*)
echo -e "\nCancelled profile creation";;
"")
echo -e "\nCancelled profile creation";;
*)
echo -e "\e[31mPlease answer yes or no.\e[0m";;
esac
fi;;
default)
local selected_profile=$2;
if [[ -n $selected_profile ]]; then
if [[ -f "$YACM_PROFILES_DIR/$selected_profile.yml" ]]; then
basename "$YACM_PROFILES_DIR"/"$selected_profile".yml .yml > \
"$YACM_DIR"/default_profile;
echo -e "\nSuccessfully set default profile to \e[32m$selected_profile\e[0m!";
print_default_warning;
else
echo -e "\e[31mERROR: provided profile doesn't exist!.\e[0m";
echo "Please specify a profile that exists.";
echo -e "To see profiles that exists run: \e[32m yacm profile list\e[0m.";
fi
else
echo -e "Currently the following profiles exist:";
profile list;
echo -e "\nPlease type the profile you would like to set as the default";
read -rp "or type the name of a profile you would like to create: " selected_profile;
if [[ -f "$YACM_PROFILES_DIR/$selected_profile.yml" ]]; then
basename "$YACM_PROFILES_DIR"/"$selected_profile".yml .yml > \
"$YACM_DIR"/default_profile;
print_default_warning;
else
echo -e "\n\e[31mWarning:\e[0m Profile doesn't currently exist."
read -rp "Would you like to create it now? (Y/n): " yn;
case $yn in
[Yy]*)
echo "";
profile create "$selected_profile";;
[Nn]*)
echo "Keeping current default profile...";;
"")
echo "";
profile create "$selected_profile";;
*)
echo -e "\e[31mPlease answer yes or no.\e[0m";;
esac
fi
fi;;
delete|--delete)
if [ -n "$(ls "$YACM_PROFILES_DIR"/*.yml 2>/dev/null)" ]; then
if [ -z "$2" ]; then
echo "The following profiles currently exist:";
profile list;
echo "";
read -rp "Which profile would you like to delete?: " profile_name;
else
profile_name=$2;
fi
if [[ -f $YACM_PROFILES_DIR/$profile_name.yml ]]; then
rm "$YACM_PROFILES_DIR"/"$profile_name".yml;
if [[ $(cat "$YACM_DIR"/default_profile) == "$profile_name" ]]; then
rm "$YACM_DIR"/default_profile;
fi
echo -e "Profile \e[32m$profile_name\e[0m deleted!";
else
echo -e "\e[31mInvalid profile name!\e[0m";
fi
else
echo -e "\e[31mNo profiles currently exist!\e[0m";
fi;;
*)
echo -e "\e[31mUnrecognized subcommand for profile command.\e[0m\n";
profile_help;;
esac
else
profile_help;
fi
}
function version() {
echo "YACM: Yet Another Configuration Manager";
echo "Version: v$YACM_VERSION";
}
function set_hostname() {
local __hostname=$1;
echo -e "\nSetting hostname to: \e[32m$__hostname\e[0m...";
$SUDO sh -c "echo $__hostname > /etc/hostname";
}
function install_packages() {
local __package_manager=$1;
local __packages=$2;
if [[ -n $__packages ]]; then
case $__package_manager in
apt|apt-get)
$SUDO apt update && $SUDO apt --yes install $__packages;;
dnf)
$SUDO dnf install -y $__packages;;
pacman)
$SUDO pacman -Sy $__packages --noconfirm --needed;;
paru)
paru -Sy $__packages --noconfirm --needed --skipreview;;
yay)
yay -Sy $__packages --noconfirm --needed;;
yum)
$SUDO yum install -y $__packages;;
*)
echo -e "\e[31mUnsupported package manager defined!\e[0m";
print_supported_package_managers;;
esac
else
echo "No packages provided. Skipping over package installation...";
fi
}
function install_flatpaks() {
local __flatpaks=$1
if [[ -n $__flatpaks ]]; then
flatpak install --noninteractive $__flatpaks;
else
echo "No flatpaks provided. Skipping over flatpak package installation...";
fi
}
function enable_services() {
local __init_system=$1;
local __services=$2;
if [[ -n $__services ]]; then
case $__init_system in
systemd)
for service in $__services; do
echo -e "Enabling service: \e[32m$service\e[0m ...";
$SUDO systemctl enable --now $service;
done;;
runit)
for service in $__services; do
if [ ! -L /run/runit/service/"$service" ]; then
echo -e "Enabling service: \e[32m$service\e[0m...";
$SUDO ln -s /etc/runit/sv/"$service" /run/runit/service/"$service";
else
echo -e "\e[32m$service\e[0m already enabled, skipping...";
fi
done;;
*)
echo -e "\e[31mUnsupported init_system defined!\e[0m";
print_supported_init_systems;;
esac
else
echo "No services specified. Skipping over enabling services...";
fi
}
function run_scripts() {
local phase=$1;
local profile=$2;
# Add YACM_SCRIPTS_DIR to PATH for bootstrap
PATH=$PATH:$YACM_SCRIPTS_DIR
if [[ $(grep "^$phase" "$YACM_PROFILES_DIR"/"$profile".yml) ]]; then
local awk_cmd="awk '/^[^ ]/{ f=/^$phase:/; next } f{ for (i=2; i<=NF; i++) printf \"%s%s\", \$i, (i<NF ? OFS : ORS)}'"
eval $awk_cmd "$YACM_PROFILES_DIR"/"$profile".yml | while read script; do
echo -e "\nRunning: \e[32m$script\e[0m...";
eval $script;
done
else
echo -e "\e[31mNo $phase defined. Skipping...\e[0m";
fi
}
function bootstrap_prompt() {
if [[ $SKIP_CONFIRMATION == 0 ]]; then
echo -e "\nProfile: \e[32m$1\e[0m selected.";
read -rp "Would you like to proceed? (Y/n): " yn;
case $yn in
[Yy]*)
bootstrap_system "$1";;
[Nn]*)
echo -e "\e[31mAborting system bootstrap...\e[0m";;
"")
bootstrap_system "$1";;
*)
echo -e "\e[31mPlease answer yes or no.\e[0m";;
esac
else
bootstrap_system "$1";
fi
}
function bootstrap_system() {
local hostname;
local package_manager;
local packages;
local init_system;
local services;
echo -e "\nBootstrapping system with profile: \e[32m$1\e[0m...";
hostname="$(sed -n -e '/^hostname:/p' "$YACM_PROFILES_DIR"/"$1".yml | awk '{print $2}')";
echo -e "\n\e[32mSetting hostname if specified:\e[0m";
set_hostname "$hostname";
echo -e "\n\e[32mRunning initial scripts if specified:\e[0m";
run_scripts "initial_scripts" "$1";
echo -e "\n\e[32mInstalling specified packages with specified package manager:\e[0m";
package_manager="$(sed -n -e '/^package_manager:/p' "$YACM_PROFILES_DIR"/"$1".yml | awk '{print $2}')";
packages="$(awk '/^[^ ]/{ f=/^packages:/; next } f{ print packages, $2 }' "$YACM_PROFILES_DIR"/"$1".yml | tr -d '\n' | sed -n -e 's/^ //' -e 's/ $//p')";
install_packages "$package_manager" "$packages";
echo -e "\n\e[32mInstalling specified flatpak packages:\e[0m";
flatpaks="$(awk '/^[^ ]/{ f=/^flatpak:/; next } f{ print flatpak, $2 }' "$YACM_PROFILES_DIR"/"$1".yml | tr -d '\n' | sed -n -e 's/^ //' -e 's/ $//p')";
install_flatpaks "$flatpaks";
echo -e "\n\e[32mRunning midway scripts if specified:\e[0m";
run_scripts "midway_scripts" "$1";
echo -e "\n\e[32mEnabling specified services with specified init system:\e[0m";
init_system="$(sed -n -e '/^init_system:/p' "$YACM_PROFILES_DIR"/"$1".yml | awk '{print $2}')";
services="$(awk '/^[^ ]/{ f=/^services:/; next } f{ print services, $2 }' "$YACM_PROFILES_DIR"/"$1".yml | tr -d '\n' | sed -n -e 's/^ //' -e 's/ $//p')";
enable_services "$init_system" "$services";
echo -e "\n\e[32mRunning final scripts if specified:\e[0m";
run_scripts "final_scripts" "$1";
echo -e "\n\e[32mBootstrap complete!\e[0m";
}
function profile_create() {
local profile_name=$1;
local init_system="";
local package_manager="";
local packages="";
while [[ -z $host_name ]]; do
echo "";
read -rp "What will the hostname of this device be?: " host_name;
if [[ -z $host_name ]]; then
echo "Please provide a hostname.";
fi
done
select_package_manager package_manager;
select_packages packages;
select_init_system init_system;
select_services services;
cat >"$YACM_PROFILES_DIR"/"$profile_name".yml <<EOF
---
name: ${profile_name}
hostname: ${host_name}
# Here you can define any custom made executable scripts or commands that
# you would like to have run before moving on to package installation.
# initial_scripts:
# -
package_manager: ${package_manager}
packages:${packages}
# Here you can define any flatpak packages you want to install
# flatpak:
# -
# Here you can define any custom made executable scripts or commands that
# you would like to have run before moving on to enabling services.
# midway_scripts:
# -
init_system: ${init_system}
services:${services}
# Here you can define any custom made executable scripts or commands that
# you would like to have run as a final bootstrap
# step.
# final_scripts:
# -
EOF
echo -e "\nCreated \e[32m$profile_name\e[0m profile!";
if [[ ! -f $YACM_DIR/default_profile ]]; then
echo -e "\nIt appears no default profile is currently set.";
read -rp "Would you like to set one now? (Y/n)" yn;
case $yn in
[Yy]*)
profile default;;
[Nn]*)
echo -e "To set a profile as default later please run: \e[32m yacm profile default\e[0m";;
"")
profile default;;
*)
echo -e "\e[31mPlease answer yes or no.\e[0m";;
esac
fi
}
function print_supported_package_managers() {
echo -e "\nyacm currently supports the following package managers:";
echo " 1) apt";
echo " 2) dnf";
echo " 3) pacman";
echo " 4) paru";
echo " 5) yay";
echo " 6) yum";
}
function select_package_manager() {
local __package_manager=$1;
while [[ -z $selected_package_manager ]]; do
print_supported_package_managers;
read -rp "Which package manager would you like to use?: " prompt;
case $prompt in
"1"|"apt"|"apt-get")
local selected_package_manager="apt";;
"2"|"dnf")
local selected_package_manager="dnf";;
"3"|"pacman")
local selected_package_manager="pacman";;
"4"|"paru")
local selected_package_manager="paru";;
"5"|"yay")
local selected_package_manager="yay";;
"6"|"yum")
local selected_package_manager="yum";;
*)
echo -e "\e[31mPlease select a supported package manager\e[0m";;
esac
done
eval "$__package_manager"="'$selected_package_manager'";
}
function select_packages() {
local __packages=$1;
local package_list="";
echo "";
read -rp "Please type the package you would like to install or type done to finish: " prompt;
while [[ $prompt != "done" ]]; do
if [[ -n $prompt ]]; then
package_list="$package_list
- $prompt";
fi
read -rp "Addtional packages (type done to finsh): " prompt;
done
eval "$__packages"="'$package_list'";
}
function print_supported_init_systems() {
echo -e "\nyacm currently supports the following init system";
echo " 1) systemd";
echo " 2) runit";
}
function select_init_system() {
local __init_system=$1;
while [[ -z $selected_init_system ]]; do
print_supported_init_systems;
read -rp "Which init system would you like to use?: " prompt;
case $prompt in
"1"|"systemd")
selected_init_system="systemd";;
"2"|"runit")
selected_init_system="runit";;
*)
echo -e "\e[31mPlease select a supported init system\e[0m";;
esac
done
eval "$__init_system"="'$selected_init_system'";
}
function select_services() {
local __services=$1;
local service_list="";
echo "";
read -rp "Please type the name of a service you would like to enable or type done to finish: " prompt;
while [[ $prompt != "done" ]]; do
if [[ -n $prompt ]]; then
service_list="$service_list
- $prompt";
fi
read -rp "Addtional services (type done to finsh): " prompt;
done
eval "$__services"="'$service_list'";
}
function qualify_path() {
local path="$1";
if [ "$path" = "." ]; then
path="$PWD";
elif [[ "$path" != /* ]]; then
path="$PWD/${path#./}";
fi
echo "$path";
}
function set_global_args() {
# global arguments are removed before the main processing is done
MAIN_ARGS=()
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
--skip-confirmation)
SKIP_CONFIRMATION=1;;
--yacm-dir) # override the standard YACM_DIR
if [ -z "$2" ]; then
echo -e "\e[31mWarning! You can't specify an empty \e[32myacm-dir\e[31m path\e[0m";
exit 1;
fi
YACM_DIR="$(qualify_path "$2" "yacm-dir")";
shift;;
--yacm-base-dir) # override the standard YACM_BASE_DIR
if [ -z "$2" ]; then
echo -e "\e[31mWarning! You can't specify an empty \e[32myacm-base-dir\e[31m path\e[0m";
exit 1;
fi
YACM_BASE_DIR="$(qualify_path "$2" "data")";
shift;;
--yacm-profiles-dir) # override the standard YACM_PROFILES_DIR
if [ -z "$2" ]; then
echo -e "\e[31mWarning! You can't specify an empty \e[32myacm-profiles-dir\e[31m path\e[0m";
exit 1;
fi
YACM_PROFILES_DIR="$(qualify_path "$2")";
shift;;
--yacm-scripts-dir) # override the standard YACM_SCRIPTS_DIR
if [ -z "$2" ]; then
echo -e "\e[31mWarning! You can't specify an empty \e[32myacm-scripts-dir\e[31m path\e[0m";
exit 1;
fi
YACM_SCRIPTS_DIR="$(qualify_path "$2")";
shift;;
*) # main arguments are kept intact
MAIN_ARGS+=("$1");;
esac
shift
done
}
function set_yacm_defaults() {
# only set SKIP_CONFIRMATION if it hasn't already be set
if [ -z "$SKIP_CONFIRMATION" ]; then
SKIP_CONFIRMATION=0;
fi
# only set YACM_BASE_DIR if it hasn't already be set
if [ -z "$YACM_BASE_DIR" ]; then
local base_yacm_dir="${XDG_CONFIG_DIR:-$HOME/.config}";
YACM_BASE_DIR="${base_yacm_dir}";
fi
# only set YACM_DIR if it hasn't already be set
if [ -z "$YACM_DIR" ]; then
YACM_DIR="${YACM_BASE_DIR}/yacm";
fi
# only set YACM_PROFILES_DIR if it hasn't already be set
if [ -z "$YACM_PROFILES_DIR" ]; then
YACM_PROFILES_DIR="${YACM_DIR}/profiles";
fi
# only set YACM_SCRIPTS_DIR if it hasn't already be set
if [ -z "$YACM_SCRIPTS_DIR" ]; then
YACM_SCRIPTS_DIR="${YACM_DIR}/scripts";
fi
}
function load_config() {
local config_file_yacm_base_dir;
local config_file_yacm_dir;
local config_file_yacm_profiles_dir;
local config_file_yacm_scripts_dir;
# Create the YACM_DIR & YACM_BASE_DIR if they doesn't exist yet
[ -d "$YACM_BASE_DIR" ] || mkdir -p "$YACM_BASE_DIR"
[ -d "$YACM_DIR" ] || mkdir -p "$YACM_DIR"
if [[ ! -f $YACM_DIR/config.yml ]]; then
cat >"$YACM_DIR"/config.yml <<EOF
---
# Configuration for yacm
# Uncomment any of the following to override the defaults for these variables
# yacm_base_dir: ${XDG_CONFIG_DIR:-$HOME/.config}
# yacm_dir: ${YACM_BASE_DIR}/yacm
# yacm_profiles_dir: ${YACM_DIR}/profiles
# yacm_scripts_dir: ${YACM_DIR}/scripts
EOF
else
config_file_yacm_base_dir="$(sed -n -e '/^yacm_base_dir:/p' "$YACM_DIR"/config.yml | awk '{print $2}')";
config_file_yacm_dir="$(sed -n -e '/^yacm_dir:/p' "$YACM_DIR"/config.yml | awk '{print $2}')";
config_file_yacm_profiles_dir="$(sed -n -e '/^yacm_profiles_dir:/p' "$YACM_DIR"/config.yml | awk '{print $2}')";
config_file_yacm_scripts_dir="$(sed -n -e '/^yacm_scripts_dir:/p' "$YACM_DIR"/config.yml | awk '{print $2}')";
YACM_BASE_DIR="${config_file_yacm_base_dir:-$YACM_BASE_DIR}";
YACM_DIR="${config_file_yacm_dir:-$YACM_DIR}";
YACM_PROFILES_DIR="${config_file_yacm_profiles_dir:-$YACM_PROFILES_DIR}";
YACM_SCRIPTS_DIR="${config_file_yacm_scripts_dir:-$YACM_SCRIPTS_DIR}";
fi
}
set_global_args "$@";
set_yacm_defaults;
load_config;
main "${MAIN_ARGS[@]}";