about summary refs log tree commit diff stats
path: root/site-scan
blob: fd89e7b630eeed1637a7fe7df70c7544a87032d7 (plain) (blame)
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
# This script looks at each directory in /var/www/users and
# writes its name into a file name `sites` if it is not empty.

# Helper function to make a rolling backup of a file.
function roll_files {
    BASE=$1
    INDEX=$(($2-1))
    while [[ $INDEX -ge 1 ]]; 
    do 
        if [ -f "$BASE.$INDEX" ]; then
            NEXT=$((INDEX+1))
            mv "$BASE.$INDEX" "$BASE.$NEXT"
        fi
        INDEX=$((INDEX-1))
    done
    if [ -f $BASE ]; then
        mv "$BASE" "$BASE.1"
    fi
}

# Helper function to determine if a directory is empty
function is_empty {
    [ -z "$(find $1/. -maxdepth 1 -name . -o -print 2>/dev/null | head -n 1)" ]
}

FILE=${1:-sites}
BU_COUNT=${2:-5}

roll_files $FILE $BU_COUNT 

for d in /var/www/users/*; do
    if [ -d $d ] && $( ! is_empty $d ); then 
        print $( echo $d | sed -e 's?^.*/??' ) >> $FILE
    fi
done