+ 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"
+ vsite="$3"
+ venabled="$3.conf"
+ [ -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/$venabled" -a ! -e "$venabledir/$venabled" \) ]; then
+
+ cp_echo "CN: Adding $vhost VirtualHost."
+ out=$(mktemp $vhostdir/$venabled.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/$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
+}