-
Notifications
You must be signed in to change notification settings - Fork 0
/
autopache.sh
executable file
·143 lines (129 loc) · 3.52 KB
/
autopache.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
#!/bin/bash
#Autopache - automatically setup a vhost for you, right here
echo "Autopache, version 1.1. Author: Dan Dart. License: MIT"
if [ "--help" == "$1" ]
then
echo "Syntax: $0 yourlocaldomain.local"
echo "Note: Must be run as root as it modifies your apache config and hosts file."
echo "Parameter 3 (optional):"
echo " --delete - deletes the vhost"
echo " --open - opens in browser"
echo "Other options:"
echo "$0 --remove - removes the program keeping the configuration file intact."
echo "$0 --purge - removes both the program and the configuration file."
exit 0
fi
if [ "0" -ne "$UID" ]
then
echo "This program must be run as root (as it requires permissions to edit apache, hosts & restart apache)"
exit 1
fi
if [ "--remove" == "$1" ]
then
echo "REMOVING Autopache..."
read -r -p "Are you sure? This will remove Autopache from the system directories, keeping the configuration file intact [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
rm /usr/bin/autopache.sh
echo Done.
exit 0
else
echo Aborted.
exit 1
fi
fi
if [ "--purge" == "$1" ]
then
echo "PURGING Autopache..."
read -r -p "Are you sure? This will remove Autopache and its configuration file from the system directories [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
rm /usr/bin/autopache.sh
rm /etc/autopache.conf
echo Done.
exit 0
else
echo Aborted.
exit 1
fi
fi
SITENAME=$1
if [ -z "$1" ]
then
echo "Usage: autopache.sh server-name-here.tld [--delete|--open]"
exit 255
fi
DIRENTRY="{{DIR}}"
CURDIR=$(pwd)
NAMEENTRY="{{NAME}}"
LOGPATHENTRY="{{LOGPATH}}"
RELEASE=`cat /etc/*release`
# Now determine the distro name and set VHOSTDIR as appropriate
if [[ $RELEASE == *Ubuntu* || $RELEASE == *Debian* ]]
then
VHOSTDIR=/etc/apache2/sites-available
RELEASENAME=Debian
SUFFIX=.conf
LOGPATH=/var/log/apache2
elif [[ $RELEASE == *Gentoo* || $RELEASE == *Funtoo* ]]
then
VHOSTDIR=/etc/apache2/vhosts.d
RELEASENAME=Gentoo
SUFFIX=.conf
LOGPATH=/var/log/apache2
elif [[ $RELEASE == *Amazon* ]]
then
VHOSTDIR=/etc/httpd/conf.d
RELEASENAME=Amazon
SUFFIX=.conf
LOGPATH=/var/log/httpd
else
echo "Unknown distribution release. Please contact the author at [email protected]"
exit 1
fi
if [ "--delete" == "$2" ]
then
if [[ $RELEASENAME == Debian ]]
then
a2dissite $SITENAME
fi
rm $VHOSTDIR/$SITENAME$SUFFIX
sed -i "s%^.*$SITENAME.*$%%g" /etc/hosts
if [[ $RELEASENAME == Amazon ]]
then
/etc/init.d/httpd restart
else
/etc/init.d/apache2 restart
fi
exit 0
fi
if [ -e ./autopache.conf ]
then
cp ./autopache.conf $VHOSTDIR/$SITENAME$SUFFIX
elif [ -e /etc/autopache.conf ]
then
cp /etc/autopache.conf $VHOSTDIR/$SITENAME$SUFFIX
else
echo "You silly billy - I can't find autopache.conf in /etc or the current directory."
fi
sed -i "s%$DIRENTRY%$CURDIR%g" $VHOSTDIR/$SITENAME$SUFFIX
sed -i "s%$NAMEENTRY%$SITENAME%g" $VHOSTDIR/$SITENAME$SUFFIX
sed -i "s%$LOGPATHENTRY%$LOGPATH%g" $VHOSTDIR/$SITENAME$SUFFIX
if [[ $RELEASENAME == Debian ]]
then
a2ensite $SITENAME
fi
if [[ $RELEASENAME == Amazon ]]
then
/etc/init.d/httpd restart
else
/etc/init.d/apache2 restart
fi
echo 127.0.0.1 $SITENAME >> /etc/hosts
if [ "--open" == "$2" ]
then
USER=$(who am i | gawk '{print $1}')
su - $USER xdg-open http://$SITENAME/
fi