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