~inspirated/arsenal/send-attachments-lpltk

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Configuration
if [ -f /etc/arsenal/arsenal.conf ] ; then
    . /etc/arsenal/arsenal.conf
fi
if [ -f /etc/arsenal/site.conf ] ; then
    . /etc/arsenal/site.conf
fi

# Defaults
CYCLE=0

# Convenience date strings
THISYEAR=`date +%Y`
TODAY=`date +%Y%m%d`
NOW=`date +%H%M`
Q=$(( 1 + ( `date +%m` - 1) / 3 ))
QUARTER=${THISYEAR}-Q${Q}
QSTART=(0000 0101 0401 0701 1001)
QEND=(0000 0331 0630 0930 1231)
QUARTER_START=`date +%y`"${QSTART[$Q]}"
QUARTER_END=`date +%y`"${QEND[$Q]}"

dbg() {
    if [ $DEBUG -gt 0 ]; then
        echo $1
    fi
}

warn() {
    echo "Warning:  $1" 1>&2
}

die() {
    msg=$1
    code=${2:-1}

    echo "Error:  $msg" 1>&2
    exit $code
}

# Runs a script, captures its output, and stores it in the given file in
# the website.
capture() {
    retr=$1
    dest=$2
    file=$3

    if [ -z "$retr" -o -z "$file" ]; then
        dbg "Invalid arguments to capture()"
        return 1
    fi

    if [ ! -d ${WORK_DIR} ] ; then
        mkdir ${WORK_DIR}
        retval=$?
        if [ $retval -ne 0 ]; then
            die "($retval) Could not create directory ${WORK_DIR}" 1
        fi
    fi

    ftmp=$(tempfile -d ${WORK_DIR} -p ${file})
    dbg "Creating $file at ${ftmp}"
    $retr > ${ftmp}
    retval=$?
    if [ $retval -ne 0 ]; then
        rm -f ${ftmp}

        if [ $retval -eq 7 ]; then
            # To us retval 7 means "Transient launchpad failure" so don't send
            # an error message in this case.
            exit $retval

            # TODO:  Check the timestamp on the last update to the file,
            #        and if it has not been updated within the past day,
            #        issue an error message

        else
            die "($retval) Could not create ${file} at ${ftmp}" 2

        fi
    fi
    if [ ! -s ${ftmp} ]; then
        die "(0) Failed to generate ${file} at ${ftmp}" 2
    fi

    if [ ! -d ${WEBSITE_DIR}/${dest} ] ; then
        mkdir -p ${WEBSITE_DIR}/${dest}
        retval=$?
        if [ $retval -ne 0 ]; then
            rm -f ${ftmp}
            die "($retval) Could not create directory ${WEBSITE_DIR}/${dest}" 3
        fi
    fi

    dbg "Installing new $file into site..."
    cp ${ftmp} ${WEBSITE_DIR}/${dest}/${file}
    retval=$?
    if [ $retval -ne 0 ]; then
        die "($retval) Could not install ${ftmp} to ${dest}" 4
    fi
    rm -f ${ftmp}

    dbg "Setting permissions on $file..."
    chmod a+r ${WEBSITE_DIR}/${dest}/${file}
    retval=$?
    if [ $retval -ne 0 ]; then
        die "($retval) Could not set permissions on ${file}" 5
    fi
}

capture_data() {
    capture "$1" "Data/" "$2"
}

relink_current() {
    source=$1
    dest=$2
    link=$3

    dbg "Relinking $link to $source..."
    ln -fs $source  ${WEBSITE_DIR}/${dest}/${link}
    retval=$?
    if [ $retval -ne 0 ]; then
        die "($retval) Could not relink file" 10
    fi
}


# Standard command line option handling
while getopts "c:" opt $*; do
    case "$opt" in
        c) CYCLE=2 ;;
    esac
done