#!/bin/bash

mycmd=$1

CYAN='\033[0;36m'
NC='\033[0m'

if [ $mycmd = help ]
then
echo "cmds:"
echo "./myctl status"
echo "./myctl permit_ar"
echo "./myctl permit_aw"
echo "./myctl permit_awf"
echo "./myctl ftpupdate"
echo "./myctl ftpupdate"
echo "------"
fi


if [ $mycmd = status ]
then
echo "---------"
echo "---------"
df -h
echo "---------"
netstat -t
echo "---------"
echo -e "SELinux ${CYAN} $(getenforce) ${NC}"
echo "---------"
echo "MySQL files open:"
lsof -u mysql | wc -l 
echo "---------"
echo -e "httpd.service ${CYAN} $(systemctl is-active httpd.service) ${NC}"
echo -e "mysqld.service ${CYAN}  $(systemctl is-active mariadb.service)${NC}"
echo -e "sshd.service  ${CYAN} $(systemctl is-active sshd.service)${NC}"
echo "---------"
echo -e "vsftpd.service ${CYAN}  $(systemctl is-active vsftpd.service)${NC}"
echo -e "firewalld.service ${CYAN}  $(systemctl is-active firewalld.service)${NC}"
echo -e "sshd.service ${CYAN}  $(systemctl is-active sshd.service)${NC}"
#echo -e "xrdp.service ${CYAN}  $(systemctl is-active xrdp.service)${NC}"
echo -e "crond.service ${CYAN}  $(systemctl is-active crond.service)${NC}"
#echo -e "postfix.service ${CYAN}  $(systemctl is-active postfix.service)${NC}"
#echo -e "dovecot.service ${CYAN}  $(systemctl is-active dovecot.service)${NC}"
fi





if [ $mycmd = start ]
then
apachectl start
systemctl start mysqld.service
systemctl start vsftpd.service
echo "started apache,mysql,ftp"
fi

if [ $mycmd = autostart ]
then
systemctl enable httpd.service
systemctl enable mysqld.service
systemctl enable vsftpd.service
fi


if [ $mycmd = stop ]
then
apachectl stop
systemctl stop mysqld.service
systemctl stop vsftpd.service
echo "services stoped"
fi


if [ $mycmd = restart ]
then
apachectl restart
systemctl restart mysqld.service
systemctl restart vsftpd
#systemctl restart postfix
#systemctl restart dovecot
echo "restarted apache,mysql,ftp,post"
fi


if [ $mycmd = ftpupdate ] 
then
db_load -T -t hash -f /etc/vsftpd/credentials.txt /etc/vsftpd/credentials.db
echo "updated"
fi


if [ $mycmd = ftplist ]
then
db_dump -p /etc/vsftpd/credentials.db
fi




#php70w-pear	


#if [ $mycmd = insmysql ]
#then
#yum-config-manager --disable mysql57-community
#yum-config-manager --disable mysql56-community
#yum-config-manager --enable mysql55-community
#yum --showduplicates list mysql-community-server
#yum install mysql-community-server
#mysql_secure_installation
#fi


if [ $mycmd = delmail ]
then
echo 'd *' | mail -N
fi


if [ $mycmd = xpermit ]
then
# Ownership
sudo chown apache:apache -R /var/www/html/xprinta
cd /var/www/html/xprinta
# File permissions, recursive
find . -type f -exec chmod 0644 {} \;
# Dir permissions, recursive
find . -type d -exec chmod 0755 {} \;
# SELinux serve files off Apache, resursive
sudo chcon -t httpd_sys_content_t /var/www/html/xprinta -R
# Allow write only to specific dirs
sudo chcon -t httpd_sys_rw_content_t /var/www/html/xprinta/orders -R
echo "updated"
fi

if [ $mycmd = fpermit ]
then
# Ownership
sudo chown apache:apache -R /var/www/html/files.iprs.ru
cd /var/www/html/files.iprs.ru
# File permissions, recursive
find . -type f -exec chmod 0644 {} \;
# Dir permissions, recursive
find . -type d -exec chmod 0755 {} \;
# SELinux serve files off Apache, resursive
sudo chcon -t httpd_sys_content_t /var/www/html/files.iprs.ru -R
echo "updated"
fi


if [ $mycmd = permit_logs ]
then
# Ownership
sudo chown apache:apache -R /var/log/httpd
cd /var/log/httpd
# File permissions, recursive
find . -type f -exec chmod 0644 {} \;
# Dir permissions, recursive
find . -type d -exec chmod 0755 {} \;
# SELinux serve files off Apache, resursive
sudo chcon -t httpd_sys_content_t /var/log/httpd -R
# Allow write only to specific dirs
sudo chcon -t httpd_sys_rw_content_t /var/log/httpd -R
sudo chcon -t httpd_sys_rw_content_t /var/log/httpd -R
fi


if [ $mycmd = permit_ar ]
then
# Ownership
sudo chown apache:apache -R $2
cd $2
# File permissions, recursive
find . -type f -exec chmod 0644 {} \;
# Dir permissions, recursive
find . -type d -exec chmod 0755 {} \;
# SELinux serve files off Apache, resursive
sudo chcon -t httpd_sys_content_t $2 -R
echo "Permited"
fi

if [ $mycmd = permit_ip ]
then
# Ownership
RULE='rule family="ipv4" source address="'$2'" accept'
sudo firewall-cmd --zone=public --add-rich-rule="$RULE"
echo $RULE
echo "IP allowed"
fi


if [ $mycmd = permit_aw ]
then
# Ownership
sudo chown apache:apache -R $2
cd $2
# File permissions, recursive
find . -type f -exec chmod 0644 {} \;
# Dir permissions, recursive
find . -type d -exec chmod 0755 {} \;
# Allow write only to specific dirs
sudo chcon -t httpd_sys_rw_content_t $2 -R
echo "Permited"
fi


if [ $mycmd = permit_awf ]
then
# Ownership
sudo chown apache:apache -R $2
# File permissions, recursive
chmod 0644 $2
# Allow write only to specific dirs
sudo chcon -t httpd_sys_rw_content_t $2
echo "Permited"
fi



if [ $mycmd = permit_mysql ]
then
# Ownership
sudo chown mysql:mysql -R /var/lib/mysql
cd /var/lib/mysql
# File permissions, recursive
find . -type f -exec chmod 0660 {} \;
# Dir permissions, recursive
find . -type d -exec chmod 0700 {} \;
# SELinux serve files off MSql, resursive
sudo chcon -t mysqld_db_t /var/lib/mysql -R
# Allow write only to specific dirs
sudo chcon -t mysqld_db_t /var/lib/mysql -R
echo "Permited"
fi


if [ $mycmd = permit_clients ]
then
# Ownership
sudo chown apache:apache -R /var/www/html/clients
cd /var/www/html/clients
# File permissions, recursive
find . -type f -exec chmod 0644 {} \;
# Dir permissions, recursive
find . -type d -exec chmod 0755 {} \;
# SELinux serve files off Apache, resursive
sudo chcon -t httpd_sys_content_t /var/www/html/clients -R
# Allow write only to specific dirs
sudo chcon -t httpd_sys_rw_content_t /var/www/html/clients/storage -R
sudo chcon -t httpd_sys_rw_content_t /var/www/html/clients/bootstrap/cache -R
#generate laravel enviroment
#cp .env.example .env
#php artisan key:generate
fi

if [ $mycmd = crontab ]
then
for user in `cat /etc/passwd | cut -d":" -f1`;
do 
crontab -l -u $user;
done
fi

if [ $mycmd = fixcron ]
then
sudo restorecon /etc/crontab
echo "CRON restored"
fi


if [ $mycmd = backupmysql ]
then
#mysqldump -u root -p cakephp | bzip2 -c > /run/media/root/Backup/mysql/cakephp-$(date +%Y-%m-%d::%H:%M:%S).sql.bz2
source backupmysql.sh
fi

if [ $mycmd = backupwww ]
then
source backupwww.sh
fi
         


if [ $mycmd = weblist ]
then
yum list installed | grep "webtatic"
fi

# firewall-cmd --zone=public --remove-port=12345/tcp --permanent
# firewall-cmd --reload
# firewall-cmd --zone=public --add-port=12345/tcp --permanent
# firewall-cmd --reload
