#! /bin/bash

DB=$(grep DB_NAME wp-config.php | cut -d \' -f 4)
DBuser=$(grep DB_USER wp-config.php | cut -d \' -f 4)
DBpass=$(grep DB_PASSWORD wp-config.php | cut -d \' -f 4)
DBhost=$(grep DB_HOST wp-config.php | cut -d \' -f 4)

function WPinstallCheck {
        if  [[ ! -f ./wp-config.php ]]; then
                echo "No WordPress installtion found in this DIR, script must be executed from user's WP directory." 1>&2
                exit 1 2> /dev/null
        fi
}

function tPrefix {
grep -i table_prefix wp-config.php | cut -d \' -f 2
}

function defaultTheme {
        mysql -u $DBuser -p$DBpass <<THM
        USE $DB
        UPDATE $(tPrefix)options SET option_value = 'twentyseventeen' WHERE option_name = 'template';
        UPDATE $(tPrefix)options SET option_value = 'twentyseventeen' WHERE option_name = 'stylesheet';
        UPDATE $(tPrefix)options SET option_value = 'Twenty Seventeen' WHERE option_name = 'current_theme';
THM
}


WPinstallCheck
if [ -d "./wp-content/themes/twentyseventeen" ]
                                then
                                        defaultTheme
                                        echo "Switched active theme to TwentySeventeen."
                                else
                                        echo "The theme TwenySeventeen is not installed, cannont switch to desired theme."
                                        exit
                        fi
