summaryrefslogtreecommitdiff
path: root/FS-Test/bin/freeside-test-start
blob: 197a84702a0f9319630aa88124d9207e196458d2 (plain)
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
#!/bin/bash

VERSION='3.8.1'

set -ae

echo "Stopping services."
sudo service freeside stop
sudo service apache2 stop

newname=freeside_`date +%Y%m%d`
sharedir=$(perl -MFS::Test -e "print FS::Test::share_dir()")

# get company_name from existing DB, strip whitespace
# (if there is no existing DB, continue anyway)
testdbmarker=$(sudo -u freeside \
  psql -tc 'select value from conf where name='\'company_name''\' | \
  sed "s/^ //" \
  || true
)

createdb=YES

if psql -l |grep -q '^ *freeside '; then
  if [ "$testdbmarker" = "Freeside Test $VERSION" ]; then
    createdb=NO
    echo "Freeside test database found."
  else
    echo "Renaming database to $newname."
    psql postgres -c "ALTER DATABASE freeside RENAME TO $newname"
  fi
fi

if [ $createdb = YES ]; then
  echo "Creating new database from stock schema."
  createdb --owner=freeside freeside
  sudo -u freeside psql freeside -q -f $sharedir/test.sql > /dev/null
fi

if [ -f /usr/local/etc/freeside/htpasswd ]; then
  newhtpasswd=htpasswd_`date +%Y%m%d`
  echo "Renaming htpasswd to $newhtpasswd."
  sudo mv /usr/local/etc/freeside/htpasswd /usr/local/etc/freeside/$newhtpasswd
fi

echo "Installing new htpasswd."
sudo cp $sharedir/htpasswd /usr/local/etc/freeside/htpasswd

newtime=$(sudo -u freeside \
  psql -tc 'SELECT to_timestamp(max(_date) + 86400) FROM cust_bill' \
)
echo "Setting clock to $newtime."
sudo date -s "$newtime"

#echo "Applying upgrades."
#sudo -u freeside freeside-upgrade test
# shouldn't happen automatically

# start Apache on a future date when all of the invoice dates are in the past
echo "Restarting Apache."
sudo service apache2 start

echo "Done."