#!/bin/bash site() { command="$1" site="$2" file="/etc/nginx/sites-available/$site" link="/etc/nginx/sites-enabled/$site" if [ -z "$site" ]; then echo "Usage: site name" 2>&1 return 1 fi case "$command" in create) sudo touch "$file"; return;; esac if [ ! -e "$file" ]; then echo "Site $site doesnt exist!" 2>&1 return 1 fi case "$command" in delete) sudo rm "$file" "$link" sudo systemctl reload nginx;; edit) sudoedit "$file" sudo systemctl reload nginx;; copy) sudo cp "$file" "/etc/nginx/sites-available/$3";; enable) if [ ! -e "$link" ]; then sudo ln -s "../sites-available/$site" "$link" sudo systemctl reload nginx fi;; disable) if [ -e "$link" ]; then sudo rm "$link" sudo systemctl reload nginx fi;; *) echo "Unknown command $command" 2>&1 return 1;; esac } _site() { if [[ "$COMP_CWORD" -eq 1 ]]; then COMPREPLY=($(compgen -W "create edit delete copy enable disable" -- "${COMP_WORDS[1]}")) elif [[ "$COMP_CWORD" -eq 2 ]]; then case "${COMP_WORDS[1]}" in edit|enable|delete|copy) dir=sites-available;; disable) dir=sites-enabled;; *) return;; esac COMPREPLY=($(cd "/etc/nginx/$dir/" && compgen -f "${COMP_WORDS[2]}")) fi } complete -F _site site