- add_namevirthost=
- mkdir_docroot=
- symlink_docroot=
- docroot=
- vhostname=
-
- while echo "x$1" | grep -q '^x-'; do
- case "$1" in
- -nvh)
- add_namevirthost=1
- shift
- ;;
- -d)
- mkdir_docroot=1
- shift
- ;;
- -s)
- shift
- symlink_docroot="$1"
- shift
- ;;
- -r)
- shift
- docroot="$1"
- if ! echo "$docroot" | grep -q /; then
- docroot="/var/www/$docroot"
- fi
- shift
- ;;
- -n)
- shift
- vhostname="$1"
- shift
- ;;
- esac
- done
-
- vhosttmpl="$1.template"
- vhost="$2"
- venabled="$3"
- [ -z "$vhostname" ] && vhostname=$(echo "$vhost"| awk -F. '{print $1}')
- force_vhost=
-
- vhostdir=$CONFDIR/sites-available
- venabledir=$CONFDIR/sites-enabled
-
- if [ ! -e "$TMPLDIR/${vhosttmpl}" ]; then
- echo "E: vhost template ${vhosttmpl} not found in $TMPLDIR!" 1>&2
- exit 2
- fi
-
- [ -z "$docroot" ] && docroot="/var/www/$vhostname.$DOMAIN"
-
- # if we were broken mid-installation, force
- if [ ! -e "$docroot" -a \( -n "$mkdir_docroot" -o -n "$symlink_docroot" \) ]; then
- force_vhost=1
- fi
-
- # add vhost if either of these is true
- # - adding is forced OR
- # - it doesn't exist
- #
- if [ -n "$force_vhost" -o \( ! -e "$vhostdir/$vhost" -a ! -e "$venabledir/$venabled" \) ]; then
-
- cp_echo "CN: Adding $vhost VirtualHost."
- out=$(mktemp $vhostdir/$vhost.XXXXXX)
- temp_files="${temp_files} ${out}"
-
- # CARNet header.
- tag_conf "$out"
-
- if [ "$add_namevirthost" ]; then
- nvh=$(awk -F'[ >]' '/^<VirtualHost/ {print $2}' $TMPLDIR/$vhosttmpl |\
- sed "s/IPADDR/$MYIP/g")
- echo "NameVirtualHost $nvh" >> $out
- fi
-
- sed "s/HOST/$vhostname/g; s/DOMAIN/$DOMAIN/g;
- s#DOCROOT#$docroot#g; s/IPADDR/$MYIP/g" < $TMPLDIR/$vhosttmpl >> $out
- cp_mv $out $vhostdir/$vhost
- chmod 644 $vhostdir/$vhost
- ln -fs ../sites-available/$vhost $venabledir/$venabled
-
- if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
- mkdir "$docroot"
- echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
- elif [ -n "$symlink_docroot" ]; then
- ln -fs "$symlink_docroot" "$docroot"
- fi
-
- need_restart=1
- fi
+ sed "s/HOST/$vhostname/g; s/DOMAIN/$DOMAIN/g;
+ s#DOCROOT#$docroot#g; s/IPADDR/$MYIP/g" < $TMPLDIR/$vhosttmpl >> $out
+ cp_mv $out $vhostdir/$venabled
+ chmod 644 $vhostdir/$venabled
+ a2ensite -m -q "$vsite"
+
+ if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
+ mkdir "$docroot"
+ echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
+ elif [ -n "$symlink_docroot" ]; then
+ ln -fs "$symlink_docroot" "$docroot"
+ fi
+
+ need_restart=1
+ fi
+}
+
+# backup_conf()
+#
+# Backup configuration files located in specified directory.
+#
+backup_conf () {
+ local dir file backup_dir
+
+ dir="$1"
+
+ if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
+ cp_echo "CN: Doing backup for all files in $dir"
+ for file in ${dir}/*; do
+ if [ -f "$file" ]; then
+ if [ -z "$(echo "$file" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ]; then
+ backup_dir="$BACKUPDIR/$(basename $(dirname "$file"))"
+ cp_backup_conffile -d "$backup_dir" -p "$file"
+ fi
+ fi
+ done
+ fi
+}
+
+# move_conf()
+#
+# Move configuration files from one directory to another. The .conf suffix
+# will be added. Will try to enable the configuration if -e is specified.
+#
+move_conf () {
+ local toenable ctype dir newdir file newfile
+
+ if [ "$1" = "-e" ]; then
+ toenable="$1"
+ shift
+ fi
+
+ ctype="$1"
+ dir="$2"
+ newdir="$3"
+
+ case "$ctype" in
+ site|conf)
+ # continue below
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+
+ if [ -z "$newdir" ]; then
+ newdir="$dir"
+ fi
+
+ if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
+ mkdir -p "$newdir"
+ for file in ${dir}/*; do
+ [ -z "$(echo "$file" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
+ newfile="${newdir}/$(basename "$file" .conf).conf"
+ if [ ! -e "$newfile" ]; then
+ cp_echo "CN: Preserving changes to $newfile (renamed from $file)."
+ cp_mv "$file" "$newfile"
+ if [ -n "$toenable" ]; then
+ cp_echo "CN: Enabling configuration $newfile"
+ a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
+ fi
+ need_restart=1
+ fi
+ done
+ fi
+}
+
+# rename_conf()
+#
+# Append the .conf suffix to all configuration files located in specified
+# available and enabled directories. Updated symlinks if necessary.
+#
+#
+rename_conf () {
+ local ctype adir edir afile efile newfile
+
+ ctype="$1"
+ adir="$2"
+ edir="$3"
+
+ case "$ctype" in
+ site|conf)
+ # continue below
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+
+ if [ -d "${edir}" ] && [ -n "$(ls -A ${edir}/)" ]; then
+ mkdir -p "$adir"
+ for efile in ${edir}/*; do
+ [ -z "$(echo "$efile" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
+
+ [ ! -e "${edir}/$(basename "$efile" .conf).conf" ] || continue
+
+ afile="$(readlink -q -m "$efile")"
+
+ [ "$(dirname "$afile")" = "$adir" ] || continue
+ [ "$(basename "$afile" .conf)" = "$(basename "$efile" .conf)" ] || continue
+
+ newfile="${adir}/$(basename "$afile" .conf).conf"
+ [ ! -e "$newfile" ] || continue
+
+ cp_echo "CN: Preserving changes to $newfile (renamed from $afile)."
+ cp_mv "$afile" "$newfile"
+
+ cp_echo "CN: Removing obsolete symlink $efile"
+ rm -f "$efile"
+
+ cp_echo "CN: Enabling configuration $newfile"
+ a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
+ need_restart=1
+ done
+ fi
+}
+
+# listconffiles()
+#
+# Recursively walks /etc/apache2/apache2.conf for Include and
+# IncludeOptional directives.
+# Prints all configfiles so defined.
+#
+listconffiles () {
+ local i incs
+ local base_dir="`dirname $1`"
+
+ incs=`awk 'tolower($1) ~ /include(optional)?/ { sub("/$","/*",$2); print $2; }' $1`
+ incs=`echo "$incs" | sed -r "s#^([^/])#${base_dir}/\1#"`
+ if [ -n "$incs" ]; then
+ for i in $incs; do
+ if [ -e "$i" ]; then echo "`readlink -m -q $i`"; listconffiles "$i"; fi
+ done
+ fi