#!/bin/bash
. config

install(){
clear 
echo " "
echo "Please wait the Web Manger install process start . . ."
echo " "
echo "Update system, please wait . . ."
sleep 2
clear
apt-get -y update 
apt-get -y upgrade
sleep 2
clear
echo " "
echo "Installing . . ."
echo " "
sleep 2
apt-get -y install apache2 php5 mysql-server phpmyadmin openssh-server tofrodos php5-cli mc cvs subversion autoconf libccid automake libtool gettext make tar bzip2 gzip patch ncurses-bin gcc g++ flex bison pkg-config wget libpng3 zlib1g-dev libssl-dev libssl0.9.8 libpcsclite1 libpcsclite-dev libusb-dev rrdtool cmake
sleep 2
reldir=`dirname $0`
cd $reldir
directory=`pwd`
root="${directory}"
filedir="${directory}/file"
tmpdir="${directory}/file/tmp"
cd $filedir
clear
tar xjvf libusb-1.0.8.tar.bz2 -C $tmpdir
cd $tmpdir/libusb-1.0.8
./configure --enable-shared=no
make
make install
cd $root
clear
sleep 1
cd $filedir
cp CCcam.x86 /usr/local/bin
chmod 755 /usr/local/bin/CCcam.x86
mkdir /var/www/manager
unrar x manager.rar /var/www
sleep 2
echo "Set Permission . . ."
sleep 2
mkdir /var/etc
chmod -R 755 /var/www/manager 
chmod -R 777 /var/www/manager/cccam
chmod -R 777 /var/www/manager/graph
chmod -R 777 /var/www/manager/rrd
chmod -R 777 /var/www/manager/tmp
chmod -R 777 /var/www/manager/admin/tmp
chmod -R 777 /var/www/manager/cfg
chmod -R 777 /var/www/manager/script
chmod -R 777 /usr/local/etc
chmod -R 777 /usr/local/bin
chmod -R 777 /var/etc
chown -R $user:$group /var/www/manager
chown -R $user:$group /usr/local/etc/
chown -R $user:$group /usr/local/bin/
chown -R $user:$group /var/etc/

echo "www-data ALL=(ALL) NOPASSWD:/var/www/manager/script/*
www-data ALL=(ALL) NOPASSWD:/bin/chown
www-data ALL=(ALL) NOPASSWD:/bin/chmod
www-data ALL=(ALL) NOPASSWD:/bin/rm
www-data ALL=(ALL) NOPASSWD:/usr/bin/rrdtool
www-data ALL=(ALL) NOPASSWD:/bin/touch" >> /etc/sudoers
chmod 440 /etc/sudoers

echo "0 4 * * *       root    $script_dir/check_fline_restart_cccam" >> /etc/crontab


### installin database

mysql -u$root_user -p$root_pass<<EOFMYSQL
CREATE USER '$user'@'$db_host' IDENTIFIED BY '$pass';
GRANT USAGE ON * . * TO '$user'@'$db_host' IDENTIFIED BY '$pass';
CREATE DATABASE $db_name;
GRANT ALL PRIVILEGES ON \`$db_name\` . * TO '$user'@'$db_host';
EOFMYSQL
cd ..
mysql -u$root_user -p$root_pass -h$db_host $db_name < manager_db.sql;

## Add default user to manager



## Make Config.inc.php

touch /var/www/manager/config.inc.php
echo "<?php
\$db_name = \"$db_name\";
\$db_host = \"$db_host\";
\$db_user = \"$db_user\";
\$db_pass = \"$db_pass\";

\$conn = mysql_connect( \$db_host, \$db_user, \$db_pass ) or die (\"Errore nella connessione al database, verifica nel file config.inc.php se tutto é corretto : \" . mysql_error());

mysql_select_db(\$db_name, \$conn) or die (\"Errore nel selezionare il database : \" . mysql_error());

// Change this value

// Define user and group

\$unix_user  = \"$user\";
\$unix_group = \"$group\";

// Default 1 for master server else for slave put in sequence 2 , 3 , 4 ... ecc
// You find server id from manager in the menu Server Manager
// 1 = Master Server
// 2 = Slave Server 1
// 3 = Slave Server 2
// x = for more server

\$server_ident = \"$server_ident\";

///////////////////////////////////////////////////

// Dir For Put Script

\$script_dir = \"$script_dir\";

// Dir to output CCcam.cfg Client

\$cfg_dir = \"$cfg_dir\";

// tmp folder

\$tmp_folder = \"$tmp_folder\";

// Default Config Path for oscam files

\$oscam_config = \"$oscam_config\";

// Dir of cccamphpinfo stats

\$cccam_server_info = \"$cccam_server_info\";

\$crond_path = \"$crond_path\";



// Define path for rddtools database

\$rrd_path_db = \"$rrd_path_db\";
\$rrd_path_graph = \"$rrd_path_graph\";

// Change default C Line want emu and uphops value

\$wantemu = \"$wantemu\";
\$uphops = \"$uphops\";
?>" > /var/www/manager/config.inc.php






clear
echo " "
echo "Install Complete ..."
echo " "
echo "Now configure the server from http://yourip/manager"
echo "User : root"
echo "Pass : root"
echo " "
echo "Change root password from phpmyadmin in tb_manager_user --> usr_password, set sh1 encription"
echo " "
exit 1
}


first(){
clear
echo "Welcome To CCcam Web Manager Installer"
echo "To continue please change value in the config file."
echo " "
echo -ne "Install ? : ( y|n ) "
	read install
		case $install in
				y) install;;
				n) exit 1;;
		esac
}

first
