#!/bin/bash

DIR="$PWD"

#Defaults
if [ -f "$DIR/wp-config.php" ]; then
	echo -e "The file wp-config.php already exists."
	echo -e "What do you want to do? [ b=Create Backup | o=Overwrite | a=Abort] \c"
	read choice
	choice=$(echo -e $choice | tr '[:upper:]' '[:lower:]' | head -c 1)
	if [[ "$choice" == "b" ]]; then
		name="wp-config.php.backup$(date +'%y:%m:%d:%T')"
		mv "$DIR/wp-config.php" "$DIR/$name"
		echo -e "Would you like to add this new file to .gitignore? [Y/n] \c"
		read input
		input=$(echo -e $input | tr '[:upper:]' '[:lower:]' | head -c 1)
		if [[ $input != 'n' ]];then
			echo "$name" >> .gitignore
		fi

	fi
	if [[ "$choice" == "a" ]]; then
		echo -e "Aborted by user"
		return 1
	fi
fi
echo "Creating new file"
echo "<?php" > "$DIR/wp-config.php"

if [ ! $dbname ]; then
	echo -e "Please enter the database name \c"
	read dbname
fi
echo "define('DB_NAME','$dbname');" >> "$DIR/wp-config.php"

if [ ! $dbuser ]; then
	echo -e "Please enter the database username \c"
	read dbuser
fi
echo "define('DB_USER','$dbuser');" >> "$DIR/wp-config.php"

if [ ! $dbpass ]; then
	echo -e "Please enter the database password \c"
	read dbpass
fi
echo "define('DB_PASSWORD','$dbpass');" >> "$DIR/wp-config.php"

if [ ! $dbhost ]; then
	echo -e "Please enter the database host \c"
	read dbhost
fi
echo "define('DB_HOST','$dbhost');" >> "$DIR/wp-config.php"

#Here are the defaults that we won't change
echo "define('DB_CHARSET','utf-8');" >> "$DIR/wp-config.php"
echo "define('DB_COLLATE','');" >> "$DIR/wp-config.php"

echo -e "Would you like to download new salts [Y/n] \c"
read input;
input=$(echo -e $input | tr '[:upper:]' '[:lower:]' | head -c 1)
if [ "$input" == "n" ]; then
	salts=false
else
	salts=true
fi


#generate the salts
if $salts; then
	echo -e "Getting new salts\n"
	curl -l https://api.wordpress.org/secret-key/1.1/salt/ >> "$DIR/wp-config.php"
else
	echo "define('AUTH_KEY','put your unique phrase here');" >> "$DIR/wp-config.php"
	echo "define('SECURE_AUTH_KEY','put your unique phrase here');" >> "$DIR/wp-config.php"
	echo "define('LOGGED_IN_KEY','put your unique phrase here');" >> "$DIR/wp-config.php"
	echo "define('NONCE_KEY','put your unique phrase here');" >> "$DIR/wp-config.php"
	echo "define('AUTH_SALT','put your unique phrase here');" >> "$DIR/wp-config.php"
	echo "define('SECURE_AUTH_SALT','put your unique phrase here');" >> "$DIR/wp-config.php"
	echo "define('LOGGED_IN_SALT','put your unique phrase here');" >> "$DIR/wp-config.php"
	echo "define('NONCE_SALT','put your unique phrase here');" >> "$DIR/wp-config.php"
fi


echo "\$table_prefix = 'wp_';" >> "$DIR/wp-config.php"

if [ ! $debug ]; then
	if [ "$defaults" == "true" ]; then
		debug=false
		echo -e "By default, debug is off"
		echo -e "To change, run with flag -d or --debug [user]"
		echo -e "For more options, run with flag --help\n"
	else
		echo -e "Would you like to have debug on? [y/N] \c"
		read input
		input=$(echo -e $input | tr '[:upper:]' '[:lower:]' | head -c 1)
		if [ "$input" == "y" ]; then
			debug=true
		else
			debug=false
		fi
	fi
fi


if [[ "$debug" == "true" ]] ; then
	echo "define('WP_DEBUG',true);" >> "$DIR/wp-config.php"
	echo "define('SAVEQUERIES',true);" >> "$DIR/wp-config.php"
else
	echo "define('WP_DEBUG',false);" >> "$DIR/wp-config.php"
	echo "define('SAVEQUERIES',false);" >> "$DIR/wp-config.php"
fi

echo -e "Will you be running wordpress from a wp subdirectory? [Y/n] \c"
read input
input=$(echo -e $input | tr '[:upper:]' '[:lower:]' | head -c 1)
if [[ $input == 'n' ]]; then
	subdirectory="false"
else
	subdirectory="true"
fi

# If wordpress is installed in a sub-directory, add redirects
if [[ "$subdirectory" == "true" ]]; then
	echo "define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/wp-content' );" >> "$DIR/wp-config.php"
	echo "define( 'WP_CONTENT_URL', 'http://' . \$_SERVER['HTTP_HOST'] . '/wp-content' );" >> "$DIR/wp-config.php"
fi
echo "if ( !defined('ABSPATH') )" >> "$DIR/wp-config.php"
echo "    define('ABSPATH',dirname(__FILE__) . '/'); " >> "$DIR/wp-config.php"

echo "require_once(ABSPATH . 'wp-settings.php');" >> "$DIR/wp-config.php"

