contiguous draft of "See Color" Decision Child PSA linkpost
[Ultimately_Untrue_Thought.git] / develop_server.sh
1 #!/usr/bin/env bash
2 ##
3 # This section should match your Makefile
4 ##
5 PY=${PY:-python3}
6 PELICAN=${PELICAN:-pelican}
7 PELICANOPTS=
8
9 BASEDIR=$(pwd)
10 INPUTDIR=$BASEDIR/content
11 OUTPUTDIR=$BASEDIR/output
12 CONFFILE=$BASEDIR/pelicanconf.py
13
14 ###
15 # Don't change stuff below here unless you are sure
16 ###
17
18 SRV_PID=$BASEDIR/srv.pid
19 PELICAN_PID=$BASEDIR/pelican.pid
20
21 function usage(){
22   echo "usage: $0 (stop) (start) (restart) [port]"
23   echo "This starts Pelican in debug and reload mode and then launches"
24   echo "an HTTP server to help site development. It doesn't read"
25   echo "your Pelican settings, so if you edit any paths in your Makefile"
26   echo "you will need to edit your settings as well."
27   exit 3
28 }
29
30 function alive() {
31   kill -0 $1 >/dev/null 2>&1
32 }
33
34 function shut_down(){
35   PID=$(cat $SRV_PID)
36   if [[ $? -eq 0 ]]; then
37     if alive $PID; then
38       echo "Stopping HTTP server"
39       kill $PID
40     else
41       echo "Stale PID, deleting"
42     fi
43     rm $SRV_PID
44   else
45     echo "HTTP server PIDFile not found"
46   fi
47
48   PID=$(cat $PELICAN_PID)
49   if [[ $? -eq 0 ]]; then
50     if alive $PID; then
51       echo "Killing Pelican"
52       kill $PID
53     else
54       echo "Stale PID, deleting"
55     fi
56     rm $PELICAN_PID
57   else
58     echo "Pelican PIDFile not found"
59   fi
60 }
61
62 function start_up(){
63   local port=$1
64   echo "Starting up Pelican and HTTP server"
65   shift
66   $PELICAN --debug --autoreload -r $INPUTDIR -o $OUTPUTDIR -s $CONFFILE $PELICANOPTS &
67   pelican_pid=$!
68   echo $pelican_pid > $PELICAN_PID
69   cd $OUTPUTDIR
70   $PY -m pelican.server $port &
71   srv_pid=$!
72   echo $srv_pid > $SRV_PID
73   cd $BASEDIR
74   sleep 1
75   if ! alive $pelican_pid ; then
76     echo "Pelican didn't start. Is the Pelican package installed?"
77     return 1
78   elif ! alive $srv_pid ; then
79     echo "The HTTP server didn't start. Is there another service using port" $port "?"
80     return 1
81   fi
82   echo 'Pelican and HTTP server processes now running in background.'
83 }
84
85 ###
86 #  MAIN
87 ###
88 [[ ($# -eq 0) || ($# -gt 2) ]] && usage
89 port=''
90 [[ $# -eq 2 ]] && port=$2
91
92 if [[ $1 == "stop" ]]; then
93   shut_down
94 elif [[ $1 == "restart" ]]; then
95   shut_down
96   start_up $port
97 elif [[ $1 == "start" ]]; then
98   if ! start_up $port; then
99     shut_down
100   fi
101 else
102   usage
103 fi