Only generate configuration for members who have a www directory
[public/expire-sites.git] / disable-webspace
1 #!/bin/bash
2
3 #
4 # CONFIGURATION
5 #
6 DISABLED_PAGE=/~sysadmin/expired/index.php
7
8 #
9 # Temporary files
10 #
11 EXPIRED_USERS=$(mktemp "/tmp/expired_users.XXXX")
12
13 #
14 # Identify expired accounts
15 #
16
17 # Get the current term
18 YEAR=$(date '+%Y')
19 MONTH=$(date '+%m')
20 TERM_START_MONTH=$(($MONTH - (($MONTH - 1) % 4)))
21
22 # NOTE: This is configured to give 1 months grace period
23 if [ ${TERM_START_MONTH} -eq 1 ]; then
24    if [ ${MONTH} = 1 ]; then
25       TERM_FILTER="(!(term=F$((${YEAR} - 1))))(!(term=W${YEAR}))"
26    else
27       TERM_FILTER="(!(term=W${YEAR}))"
28    fi
29 elif [ ${TERM_START_MONTH} -eq 5 ]; then
30    if [ ${MONTH} = 5 ]; then
31       TERM_FILTER="(!(term=W${YEAR}))(!(term=W${YEAR}))"
32    else
33       TERM_FILTER="(!(term=S${YEAR}))"
34    fi
35 elif [ ${TERM_START_MONTH} -eq 9 ]; then
36    if [ ${MONTH} = 9 ]; then
37       TERM_FILTER="(!(term=S${YEAR}))(!(term=F${YEAR}))"
38    else
39       TERM_FILTER="(!(term=F${YEAR}))"
40    fi
41 else
42    echo "Invalid term start month: ${TERM_START_MONTH}" >&2
43    exit 1
44 fi
45
46 # Find expired accounts
47 ldapsearch -xb "ou=People,dc=csclub,dc=uwaterloo,dc=ca" "(&${TERM_FILTER}(objectClass=member))" | grep 'uid:' | awk '{print $2}' | sort -u > ${EXPIRED_USERS}
48
49 #
50 # Generate Apache config
51 #
52 for uid in $(cat ${EXPIRED_USERS}); do
53    if [ -d /users/${uid}/www ]; then
54       echo "<Directory /users/${uid}/www>"
55       echo "    AllowOverride None"
56       echo "    RewriteEngine On"
57       echo "    RewriteRule . ${DISABLED_PAGE} [L]"
58       echo "</Directory>"
59       echo ""
60    fi
61 done
62
63 rm -f ${EXPIRED_USERS}