Alternc  latest
Alternc logiel libre pour l'hébergement
upgrade_check.sh
Go to the documentation of this file.
1 #!/bin/bash
2 
3 # this script will look for upgrade scripts in
4 # /usr/share/alternc/install/upgrades and execute them based on the
5 # extension
6 #
7 # an upgrade file is considered only if its basename is a version
8 # number greater than the $oldvers argument
9 
10 # remove version from filename by stripping the extension
11 strip_ext() {
12  echo $1 | sed -e 's/\.[^.]*$//' -e 's/[a-z_]*$//'
13 }
14 
15 # find the version from a filename by stripping everything but the extension
16 get_ext() {
17  echo $1 | sed 's/^.*\.\‍([^.]*\‍)$/\1/'
18 }
19 
20 
21 # Reading the current version in the DB.
22 # If the DB exist but the alternc_status table doesn't, we will initialize it below
23 # In that case we search where we upgrade from in /var/lib/alternc/backups/lastversion from debian.postinstall script
24 oldvers="`mysql --defaults-file=/etc/alternc/my.cnf --skip-column-names -e "SELECT value FROM alternc_status WHERE name='alternc_version'" 2>/dev/null||true`"
25 if [ -z "$oldvers" ]
26 then
27  # no version number, we check from /var/lib/alternc
28  if [ -f "/var/lib/alternc/backups/lastversion" ]
29  then
30  oldvers="`cat /var/lib/alternc/backups/lastversion`"
31  # this is a *version number*, not a *last upgrade script* we have this *border case*
32  if [ "$oldvers" = "3.1" ]
33  then
34  oldvers="3.1.0~c.sh"
35  fi
36  else
37  echo "##############################################"
38  echo "# NO VERSION FOUND TO UPGRADE FROM, ABORTING #"
39  echo "##############################################"
40  exit 1
41  fi
42 fi
43 
44 if [ "$oldvers" = '<unknown>' ]
45 then
46  # this is not an upgrade
47  exit 0
48 fi
49 
50 # Thanks to that, we handle alternc older than 3.1.0~b.php
51 mysql --defaults-file=/etc/alternc/my.cnf -e "CREATE TABLE IF NOT EXISTS alternc_status (name VARCHAR(48) NOT NULL DEFAULT '',value LONGTEXT NOT NULL,PRIMARY KEY (name),KEY name (name) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;"
52 mysql --defaults-file=/etc/alternc/my.cnf -e "INSERT IGNORE INTO alternc_status SET name='alternc_version',value='$oldvers';"
53 
54 . /etc/alternc/local.sh
55 
56 # the upgrade script we are considering
57 extensions="*.sql *.sh *.php"
58 cd /usr/share/alternc/install/upgrades
59 for file in $( ls $extensions | sort -n ) ; do
60  if [ -r $file ]; then
61  # the version in the filename
62  upvers=`strip_ext $file`
63  # the extension
64  ext=`get_ext $file`
65  if dpkg --compare-versions "$upvers" gt "$oldvers"; then
66  echo "Running upgrade script $file"
67  # run the proper program to interpret the upgrade script
68  case "$ext" in
69  sql)
70  ( echo "BEGIN;"
71  cat $file
72  echo "UPDATE alternc_status SET value='$file' WHERE name='alternc_version';"
73  echo "COMMIT;"
74  ) | mysql --defaults-file=/etc/alternc/my.cnf -f
75  ;;
76  php)
77  php -q $file
78  echo "UPDATE alternc_status SET value='$file' WHERE name='alternc_version';" |
79  mysql --defaults-file=/etc/alternc/my.cnf -f
80  ;;
81  sh)
82  bash $file
83  echo "UPDATE alternc_status SET value='$file' WHERE name='alternc_version';" |
84  mysql --defaults-file=/etc/alternc/my.cnf -f
85  ;;
86  *)
87  echo "skipping $file, not recognized !"
88  ;;
89  esac
90  fi
91  fi
92 done
93