updated on Sat Jan 21 20:03:50 UTC 2012
[aur-mirror.git] / zoneminder / zminit.arch
blob01b6658a79a17f061ad6c4bfb9126f97005a04ad
1 #!/bin/sh
4 ZM_CONFIG=/etc/zm.conf
8 loadcfg() {
9 if [ -f $ZM_CONFIG ]; then
10 . $ZM_CONFIG
11 else
12 echo "ERROR: $ZM_CONFIG not found."
13 exit 1
16 chkmysql(){
17 #FIXME
18 /etc/rc.d/mysqld restart || (echo "mysql is not availible" && exit 1 )
21 getmylogin(){
22 echo "Enter MySQL Administrator username"
23 echo "(Default: root and password is blank)"
24 echo -n "MySQL Admin: "
25 read MYADMIN
26 echo -n "Password: "
27 read MYPASS
28 if [ "X$MYPASS" != "X" ]; then MYPASS="-p$MYPASS"; fi
29 echo "\q" |mysql -u $MYADMIN $MYPASS || exit 0
33 checkdb()
35 # Check database exisits and version
36 exists=$(echo "show databases" |mysql -u $MYADMIN "$MYPASS" |grep zm)
37 if [ "$exists" = "zm" ]; then
38 OLD_VERSION=$(echo "select Value from Config where Name = 'ZM_DYN_DB_VERSION';" | mysql -u $MYADMIN $MYPASS zm |grep -v '^Value')
40 case $OLD_VERSION in
41 "")
42 echo "A zm database exists, but the version is unknown"
43 echo "Updating is unlikely to succeed"
45 "$ZM_VERSION")
46 echo "The zm database appears to be up to date"
47 echo "If this is incorrect, edit $ZM_CONFIG to reflect the current version"
50 echo "A database fron zm-$OLD_VERSION exists already"
52 esac
54 while [ true ]
56 echo "Choose one of the following options:"
57 echo "[U]pdate the database"
58 echo "[D]rop the old database and reinitialize"
59 echo "[E]xit and do nothing"
60 read OPTION
61 case $OPTION in
62 "U"|"u")
63 /etc/rc.d/zm restart
64 exit 0
66 "D"|"d")
67 echo "drop database zm;"|mysql -u $MYADMIN $MYPASS
68 return
70 "E"|"e")
71 exit 0
73 esac
74 done
78 checkcfg(){
79 for n in ZM_DB_HOST ZM_DB_NAME ZM_DB_USER ZM_DB_PASS; do
80 eval "val=\$$n"
81 if [ "$val" = "" ]; then
82 echo "ERROR($ZM_CONFIG): $n should not be empty."
83 echo "Enter a $n for ZM to use the Database."
84 if [ "$n" = "ZM_DB_PASS" ]; then
85 echo -n "Will not echo on screen $n : "
86 stty -echo # Turns off screen echo.
87 read newval
88 stty echo # Restores screen echo.
89 echo ""
90 ### The following can be used to generate a random password
91 # randstr newval 16
92 else
93 echo -n "$n : "
94 read newval
96 cp $ZM_CONFIG /tmp/$$ &&
97 sed 's/^'$n='.*$/'$n=$newval'/g' /tmp/$$ >$ZM_CONFIG
100 done
102 if [ "$ZM_DB_HOST" = "localhost" ]; then
103 ClientHost=localhost
104 else
105 ClientHost=`hostname`
109 reloadcfg(){
110 loadcfg
113 initdb(){
114 sql=/tmp/zm.crdb.sql
115 echo "" >$sql
116 chmod 600 $sql
118 echo "CREATE DATABASE $ZM_DB_NAME;" >>$sql
119 echo "USE $ZM_DB_NAME;" >>$sql
121 echo "GRANT all on $ZM_DB_NAME.* TO '$ZM_DB_USER'@'$ClientHost' IDENTIFIED BY '$ZM_DB_PASS';" >>$sql
123 cat $sql | mysql -B -h $ZM_DB_HOST -u $MYADMIN $MYPASS
124 rm -f $sql
126 cat $ZM_PATH_UPDATE/zm_create.sql | mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME
127 ( cd $ZM_PATH_UPDATE; perl $ZM_PATH_BIN/zmupdate.pl -f )
129 $ZM_PATH_UPDATE/customdb $ZM_DB_HOST $ZM_DB_NAME $ZM_DB_USER $ZM_DB_PASS
134 loadcfg
135 getmylogin
136 checkdb
137 checkcfg
138 reloadcfg
139 initdb