Script only partially runs on theme activation, but runs fully on deactivation?

You’re looking for this:

register_activation_hook( __FILE__, 'your_plugin_activate_function_name' );

Edit: for a theme, you can use something like this instead:

$theme_version = get_option('my_theme_version');

switch ((string) $theme_version) {
  // an early alpha... run upgrade code
  case '0.1':

  // another version... run upgrade code
  case '0.5':

  // add other cases as needed... without any break statement.

    // if we're here and in the admin area (to avoid race conditions),
    // actually save whichever changes we did
    if (is_admin()) {
      update_option('my_theme_version', '1.0');
      // do other stuff
    }

  // we're at the latest version: bail here.
  case '1.0':
    break;

  default:
    // install code goes here
}