File: /home/httpd/html/porn.tw/public_html_old/wp.sh
#!/bin/bash
#Generate a unique hash for logging
r=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c12`
#Make sure we have a name to use as a seed
if [ -z "$@" ]; then echo require a name to use as a seed for db and user names;exit 1;fi
#Using /var/tmp/installer/wordpress.latest.tar for wordpress archive, make sure its new, if not delete it and get a new one.
[ -d /var/tmp/installer ] || mkdir -p /var/tmp/installer
find /var/tmp/installer/ -mtime +1 -name wordpress.latest.tar -exec rm '{}' \;
[ -e /var/tmp/installer/wordpress.latest.tar ] && echo Already have recent wordpress archive || (wget -O /var/tmp/installer/wordpress.latest.tar.gz http://wordpress.org/latest.tar.gz;gunzip /var/tmp/installer/wordpress.latest.tar.gz)
[ -e /var/tmp/installer/wordpress.latest.tar ] || exit
#Generate a password
p=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c12`
#Use $1 as a seed for the name
t=`echo $1 | sed -e 's/\.//g' | tr '-' '_'| cut -c -10`
echo Password is $p name seed is $t
#Log it all
echo "=$r=Starting install in `pwd` on `date`==" >> /var/log/wordpress.script.log
#Dont plow over another install
if [ -e wp-config.php ]; then echo ERROR! wordpress config file detected, refusing to continue;echo ="$r"=Canceling install for pre-existing config >> /var/log/wordpress.script.log;exit 1; fi
if [ -e wp-content ]; then echo ERROR! wordpress content file detected, refusing to continue;echo ="$r"=Canceling install for pre-existing contenti >> /var/log/wordpress.script.log;exit 1; fi
#Extract the wordpress files
echo Extracting wordpress
echo ="$r"=Extracting Wordpress in `pwd` >> /var/log/wordpress.script.log
if `tar -f /var/tmp/installer/wordpress.latest.tar -x --strip-components=1`;
then
echo ="$r"=Extracting Wordpress in `pwd` complete >> /var/log/wordpress.script.log
echo Extracting Wordpress complete
else
echo ="$r"=Extracting Wordpress in `pwd` failed, trying with legacy tar options
echo failed, trying with legacy tar options
if `tar -f /var/tmp/installer/wordpress.latest.tar -x --strip-path=1`; then
echo ="$r"=Extracting Wordpress in `pwd` complete >> /var/log/wordpress.script.log
else
echo ="$r"=Extracting Wordpress completely failed in `pwd` >> /var/log/wordpress.script.log
exit 1
fi
fi
echo Writting config file
echo ="$r"=Generating configuration >> /var/log/wordpress.script.log
#Write out the configuration details
echo -e "<?php\ndefine('DB_NAME', 'database_name_here');\ndefine('DB_USER', 'username_here');\ndefine('DB_PASSWORD', 'password_here');\ndefine('DB_HOST', 'localhost');\ndefine('DB_CHARSET', 'utf8');\ndefine('DB_COLLATE', '');\n"| sed -e "s/database_name_here/""$t""_wp/g" | sed -e "s/username_here/""$t""wpuser/g" | sed -e "s/password_here/""$p""/g" > wp-config.php
#Let wordpress.org generate us some nice keys
wget -O - -q https://api.wordpress.org/secret-key/1.1/salt/ >> wp-config.php
#Set wordpress to not prompt for FTP info, which always fails, by setting FS_METHOD to direct and wrap up the config
echo -e "\$table_prefix = 'wp_';\ndefine('FS_METHOD', 'direct');\ndefine ('WPLANG', '');\ndefine('WP_DEBUG', false);\nif ( \x21defined('ABSPATH') )\ndefine('ABSPATH', dirname(__FILE__) . '/');\nrequire_once(ABSPATH . 'wp-settings.php');" >> wp-config.php
#Write a .htaccess to make links look nice
echo Writting .htaccess
echo ="$r"=Generating .htaccess >> /var/log/wordpress.script.log
echo -e "\n\n# BEGIN WordPress\n\nRewriteEngine On\nRewriteBase /\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]\n\n# END WordPress\n" >> .htaccess
#Check that we arent using an existing database
de=`mysql -e show\ databases\ like\ \""$t"_wp\" | wc -l`
if [ "$de" -lt 1 ] ;then
#Create database
echo Creating database "$t""_wp"
echo ="$r"=Creating database "$t""_wp" >> /var/log/wordpress.script.log
echo "create database ""$t""_wp" | mysql
echo granting database permissions
echo ="$r"=Granting permissions on "$t""_wp" to ""$t""wpuser@localhost >> /var/log/wordpress.script.log
echo "grant all on ""$t""_wp.* to ""$t""wpuser@localhost identified by \"""$p""\"" | mysql
else
echo error database already exists
echo ="$r"=Database "$t""_wp" already exists >> /var/log/wordpress.script.log
exit 1
fi
echo chowning document root
echo ="$r"=Setting Permissions on `pwd` >> /var/log/wordpress.script.log
chown -R --reference=./ ./
echo chmoding document root
chmod -R u=rwX,g=rX,o=rX ./
echo chowning wp-content
chmod -R u=rwX,g=rwX,o=rX ./wp-content/ ./.htaccess
unset t
unset p
unset n