import os
import subprocess
from tempfile import mkstemp
from shutil import move
from os import remove, close

################# APACHE2 ####################################

print "Checking for apache installation..."

apache2path = subprocess.Popen("which apache2", shell=True, stdout=subprocess.PIPE).stdout.read()

if "apache2" not in apache2path:
    print "installing apache web server (might take a while..)"
    print subprocess.Popen("sudo apt-get install apache2 -y", shell=True, stdout=subprocess.PIPE).stdout.read()
else:
    print "apache already installed. Skipping"
    print "-----------"

################# PHP ########################################

print "Checking for php installation..."

phpPath = subprocess.Popen("which php", shell=True, stdout=subprocess.PIPE).stdout.read()

if "php" not in phpPath:
    print "installing php  (might take a while..)"
    print subprocess.Popen("sudo apt-get install php5 libapache2-mod-php5 -y", shell=True, stdout=subprocess.PIPE).stdout.read()
else:
    print "php already installed. Skipping"

print "-----------"

################# SQLITE3 ########################################

print "Checking for sqlite3 installation..."

phpPath = subprocess.Popen("which sqlite3", shell=True, stdout=subprocess.PIPE).stdout.read()

if "sqlite3" not in phpPath:
    print "installing sqlite3  (might take a while..)"
    print subprocess.Popen("sudo apt-get install sqlite3 -y", shell=True, stdout=subprocess.PIPE).stdout.read()
else:
    print "sqlite3 already installed. Skipping"

# create temp.db

#CREATE TABLE TEMP(
#                  ...>     timestamp DATETIME DEFAULT CURRENT_TIMESTAMP PRIMARY KEY,
#                  ...>     temp REAL,
#                           location TEXT
#                  ...> );

print "-----------"

################# MOVE PHP PAGE TO RIGHT PLACE ###############

print "adding php admin page for thermometer."
print "installing php pages in /var/www/html/"

print subprocess.Popen("sudo mkdir -p /var/www/html/thermometer && sudo cp -R php/* /var/www/html/thermometer", shell=True, stdout=subprocess.PIPE).stdout.read()

print "-----------"

#################THERMO FILES, CONFIG ETC######################
thermoPath = "/home/pi/thermometer"

print "Installing thermometer files in " + thermoPath
print subprocess.Popen("mkdir -p " + thermoPath, shell=True, stdout=subprocess.PIPE).stdout.read()
if not thermoPath.endswith("/"):
	thermoPath += "/"
print subprocess.Popen("cp -R thermometer/* " + thermoPath, shell=True, stdout=subprocess.PIPE).stdout.read()


#if isServer:
#    print "Adding alarm home to admin.properties"
#    print subprocess.Popen('echo "alarm_home:' + alarmPath + '" | sudo tee /var/www/html/alarm/admin.properties', shell=True, stdout=subprocess.PIPE).stdout.read()
print "Making the thermometer application available for the php server"
print subprocess.Popen('sudo chmod 777 ' + thermoPath + '/*', shell=True, stdout=subprocess.PIPE).stdout.read()
print subprocess.Popen('sudo chmod 777 /var/www/html/thermometer/admin.properties', shell=True, stdout=subprocess.PIPE).stdout.read()

print "todo. add this to crontab: 0 4,15 * * * python /home/pi/thermometer/sense_remote_temp.py /var/www/html/thermometer 1> /home/pi/templog.txt 2> /home/pi/templog.err"

##############################################################

print
print "Congratulation! Now go to <IP of your raspberry>/thermometer and configure your thermometer."

@@ -1855,55 +1855,8 @@
 ##%0A%0A
-print %22adding php admin page for thermometer.%22%0A
 prin
@@ -1951,27 +1951,21 @@
 ww/html/
-thermometer
+mello
  && sudo
@@ -1991,27 +1991,21 @@
 ww/html/
-thermometer
+mello
 %22, shell
@@ -2075,1146 +2075,8 @@
 -%22%0A%0A
-#################THERMO FILES, CONFIG ETC######################%0AthermoPath = %22/home/pi/thermometer%22%0A%0Aprint %22Installing thermometer files in %22 + thermoPath%0Aprint subprocess.Popen(%22mkdir -p %22 + thermoPath, shell=True, stdout=subprocess.PIPE).stdout.read()%0Aif not thermoPath.endswith(%22/%22):%0A%09thermoPath += %22/%22%0Aprint subprocess.Popen(%22cp -R thermometer/* %22 + thermoPath, shell=True, stdout=subprocess.PIPE).stdout.read()%0A%0A%0A#if isServer:%0A#    print %22Adding alarm home to admin.properties%22%0A#    print subprocess.Popen('echo %22alarm_home:' + alarmPath + '%22 %7C sudo tee /var/www/html/alarm/admin.properties', shell=True, stdout=subprocess.PIPE).stdout.read()%0Aprint %22Making the thermometer application available for the php server%22%0Aprint subprocess.Popen('sudo chmod 777 ' + thermoPath + '/*', shell=True, stdout=subprocess.PIPE).stdout.read()%0Aprint subprocess.Popen('sudo chmod 777 /var/www/html/thermometer/admin.properties', shell=True, stdout=subprocess.PIPE).stdout.read()%0A%0Aprint %22todo. add this to crontab: 0 4,15 * * * python /home/pi/thermometer/sense_remote_temp.py /var/www/html/thermometer 1%3E /home/pi/templog.txt 2%3E /home/pi/templog.err%22%0A
 %0A###
@@ -2169,82 +2169,15 @@
 on! 
-Now go to %3CIP of your raspberry%3E/thermometer and configure your thermometer
+All done
 .%22%0A

install script