-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostsm.sh
118 lines (96 loc) · 2.6 KB
/
hostsm.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
#!/usr/bin/env sh
# mac OS (until 10.14.x macOS Mojave), iOS, Android, Linux: /etc/hosts
# Windows: %SystemRoot%\system32\drivers\etc\hosts
# hosts file MacOS Catalania /private/etc/hosts
# hostsm config dir: ~/.config/hostm/
usage()
{
echo "No options were passed.";
echo
echo "SYNOPSIS:"
echo
echo "hostsm [Option ...] FILE"
echo
echo "OPTIONS:"
echo
echo -e "-l, --list \t lists all available hosts files"
echo -e "-f, --file FILE \t symlinks the selected file to /etc/hosts and overwrite old one"
echo
echo "Github page: https://github.com/LibreDrip/hostsm"
exit 1
}
# _hostsm () {
# case $COMP_CWORD in
# 1) COMPREPLY=( $(compgen -W "staandda sooos" "$2") ) ;;
# 2) local IFS=$'\n'
# COMPREPLY=( $(cd ~/.config/hostsm && compgen -d "$2") ) ;;
# esac
# }
# complete -F _hostsm hostsm
# check if the user is running mac os catalania
# if [ -f /private/etc/hosts]
# then
# else
# fi
# everybody else
# detect home directory when called with sudo
#USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)
#echo $USER_HOME
if [ -d ~/.config/hostsm ] #check if config folder exists
then
cd ~/.config/hostsm
CONFIGDIR=~/.config/hostsm
else
echo "Config Folder created"
mkdir ~/.config/hostsm
fi
if [ $# -eq 0 ]; #if no option is passed
then
usage
fi
while :; do
case $1 in
-l|--list)
echo "$(ls)"
;;
-f|--file) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
input=$2
if [ -f $input ] #check if file is empty
then
# file="
# echo "$(find $CONFIGDIR -name 'input')"
# echo $CONFIGDIR
find $CONFIGDIR -name $input | while read line; do
echo $line
sudo ln -s -f $line /etc/hosts
# sudo ln -s -f $file /private/etc/hosts
# -s symbolic
# -f force (remove file)
echo "Symlink created"
done
# echo $file
exit
else
echo "Empty or non Existant File."
exit
fi
shift
else
echo 'ERROR: "--file" requires a non-empty option argument.'
fi
;;
--file=?*)
file=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--file=) # Handle the case of an empty --file=
echo 'ERROR: "--file" requires a non-empty option argument.'
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done