#!/bin/bash # Strain Data Migration Tool for Weedops # Imports strain data from CSV into WordPress custom post types # Usage: ./wp-migrate-strains.sh [wp-path] set -euo pipefail CSV_FILE="${1:-}" WP_PATH="${2:-/var/www/weedops}" if [ -z "$CSV_FILE" ]; then echo "Usage: $0 [wp-path]" echo "" echo "CSV format: name,type,thc_min,thc_max,cbd_min,cbd_max,effects,description" echo "Types: indica, sativa, hybrid" echo "" echo "Example:" echo " Blue Dream,hybrid,17,24,0.1,0.2,\"relaxed,happy,creative\",\"Popular hybrid strain\"" exit 1 fi if [ ! -f "$CSV_FILE" ]; then echo "ERROR: File not found: $CSV_FILE" exit 1 fi if ! command -v wp &> /dev/null; then echo "ERROR: WP-CLI required. Install from https://wp-cli.org/" exit 1 fi echo "=========================================" echo " Weedops Strain Data Importer" echo " $(date '+%Y-%m-%d %H:%M:%S')" echo "=========================================" echo "Source: $CSV_FILE" echo "Target: $WP_PATH" echo "" IMPORTED=0 SKIPPED=0 ERRORS=0 # Skip header line tail -n +2 "$CSV_FILE" | while IFS=',' read -r name type thc_min thc_max cbd_min cbd_max effects description; do # Clean up fields name=$(echo "$name" | sed 's/^"//;s/"$//' | xargs) type=$(echo "$type" | sed 's/^"//;s/"$//' | xargs) description=$(echo "$description" | sed 's/^"//;s/"$//') if [ -z "$name" ]; then continue fi echo -n "Importing: $name... " # Check if strain already exists EXISTING=$(wp post list --path="$WP_PATH" --post_type=strain --name="$(echo "$name" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')" --format=count 2>/dev/null || echo "0") if [ "$EXISTING" != "0" ]; then echo "SKIPPED (already exists)" ((SKIPPED++)) continue fi # Create the strain post POST_ID=$(wp post create \ --path="$WP_PATH" \ --post_type=strain \ --post_title="$name" \ --post_content="$description" \ --post_status=publish \ --porcelain 2>/dev/null || echo "0") if [ "$POST_ID" = "0" ]; then echo "ERROR" ((ERRORS++)) continue fi # Set strain type taxonomy wp term set "$POST_ID" strain_type "$type" --path="$WP_PATH" 2>/dev/null || true # Set meta fields wp post meta update "$POST_ID" thc_min "$thc_min" --path="$WP_PATH" 2>/dev/null || true wp post meta update "$POST_ID" thc_max "$thc_max" --path="$WP_PATH" 2>/dev/null || true wp post meta update "$POST_ID" cbd_min "$cbd_min" --path="$WP_PATH" 2>/dev/null || true wp post meta update "$POST_ID" cbd_max "$cbd_max" --path="$WP_PATH" 2>/dev/null || true wp post meta update "$POST_ID" effects "$effects" --path="$WP_PATH" 2>/dev/null || true echo "OK (ID: $POST_ID)" ((IMPORTED++)) done echo "" echo "=========================================" echo " Import complete" echo " Imported: $IMPORTED | Skipped: $SKIPPED | Errors: $ERRORS" echo "========================================="