static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
-   " StartBlock=%u EndBlock=%u Copy=%d Strip=%d\n";
+   " StartBlock=%u EndBlock=%u Copy=%d Strip=%d MediaId=%" lld "\n";
 
 
 /* Responses  sent to Storage daemon */
    " VolBlocks=%u VolBytes=%s VolMounts=%u VolErrors=%u VolWrites=%u"
    " MaxVolBytes=%s VolCapacityBytes=%s VolStatus=%s Slot=%d"
    " MaxVolJobs=%u MaxVolFiles=%u InChanger=%d VolReadTime=%s"
-   " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d\n";
+   " VolWriteTime=%s EndFile=%u EndBlock=%u VolParts=%u LabelType=%d"
+   " MediaId=%s\n";
 
 static char OK_create[] = "1000 OK CreateJobMedia\n";
 
 static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr)
 {
    int stat;
-   char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
+   char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50];
 
    jcr->MediaId = mr->MediaId;
    pm_strcpy(jcr->VolumeName, mr->VolumeName);
       edit_uint64(mr->VolWriteTime, ed5),
       mr->EndFile, mr->EndBlock,
       mr->VolParts,
-      mr->LabelType);
+      mr->LabelType,
+      edit_uint64(mr->MediaId, ed6));
    unbash_spaces(mr->VolumeName);
    Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
    return stat;
    POOLMEM *omsg;
    POOL_DBR pr;
    uint32_t Stripe;
+   uint64_t MediaId;
    utime_t VolFirstWritten;
 
    memset(&mr, 0, sizeof(mr));
     */
    } else if (sscanf(bs->msg, Create_job_media, &Job,
       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
-      &jm.StartBlock, &jm.EndBlock, &jm.Copy, &Stripe) == 9) {
+      &jm.StartBlock, &jm.EndBlock, &jm.Copy, &Stripe, &MediaId) == 10) {
 
       if (jcr->mig_jcr) {
          jm.JobId = jcr->mig_jcr->JobId;
-         jm.MediaId = jcr->MediaId;
       } else {
          jm.JobId = jcr->JobId;
-         jm.MediaId = jcr->MediaId;
       }
+      jm.MediaId = MediaId;
       Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
 
    {"verifyjob", store_res,     ITEM(res_job.verify_job), R_JOB, 0, 0},
    {"jobtoverify", store_res,   ITEM(res_job.verify_job), R_JOB, 0, 0},
    {"jobdefs",   store_res,     ITEM(res_job.jobdefs),    R_JOBDEFS, 0, 0},
-   {"nextpool",  store_res,     ITEM(res_job.next_pool),  R_POOL, 0, 0},
    {"run",       store_alist_str, ITEM(res_job.run_cmds), 0, 0, 0},
    /* Root of where to restore files */
    {"where",    store_dir,      ITEM(res_job.RestoreWhere), 0, 0, 0},
 
    POOL      *full_pool;              /* Pool for Full backups */
    POOL      *inc_pool;               /* Pool for Incremental backups */
    POOL      *diff_pool;              /* Pool for Differental backups */
-   POOL      *next_pool;              /* Next Pool for Migration */
    char      *selection_pattern;
    int        selection_type;
    union {
 
 {
    idpkt *ids = (idpkt *)ctx;
 
-   Dmsg3(dbglevel, "count=%d Ids=%p %s\n", ids->count, ids->list, ids->list);
    if (ids->count == 0) {
       ids->list[0] = 0;
    } else {
    }
    pm_strcat(ids->list, row[0]);
    ids->count++;
+   Dmsg3(dbglevel, "dbid_hdlr count=%d Ids=%p %s\n", ids->count, ids->list, ids->list);
    return 0;
 }
 
    
    memset(new_item, 0, sizeof(uitem));
    new_item->item = bstrdup(row[0]);
-   Dmsg1(dbglevel, "Item=%s\n", row[0]);
+   Dmsg1(dbglevel, "Unique_name_hdlr Item=%s\n", row[0]);
    item = (uitem *)list->binary_insert((void *)new_item, item_compare);
    if (item != new_item) {            /* already in list */
       free(new_item->item);
    p = ids.list;
    Jmsg(jcr, M_INFO, 0, _("The following %u JobIds will be migrated: %s\n"),
       ids.count, ids.list);
+   Dmsg2(dbglevel, "Before loop count=%d ids=%s\n", ids.count, ids.list);
    for (int i=1; i < (int)ids.count; i++) {
       JobId = 0;
       stat = get_next_jobid_from_list(&p, &JobId);
-      Dmsg2(dbglevel, "get_next_jobid stat=%d JobId=%u\n", stat, JobId);
+      Dmsg3(dbglevel, "get_jobid_no=%d stat=%d JobId=%u\n", i, stat, JobId);
       jcr->MigrateJobId = JobId;
       start_migration_job(jcr);
+      Dmsg0(dbglevel, "Back from start_migration_job\n");
       if (stat < 0) {
          Jmsg(jcr, M_FATAL, 0, _("Invalid JobId found.\n"));
          goto bail_out;
    }
    /* Basic query for names */
    Mmsg(query, query1, jcr->pool->hdr.name);
-   Dmsg1(dbglevel, "query1=%s\n", query.c_str());
+   Dmsg1(dbglevel, "get name query1=%s\n", query.c_str());
    if (!db_sql_query(jcr->db, query.c_str(), unique_name_handler, 
         (void *)item_chain)) {
       Jmsg(jcr, M_FATAL, 0,
          free(last_item->item);
          item_chain->remove(last_item);
       }
-      Dmsg1(dbglevel, "Item=%s\n", item->item);
+      Dmsg1(dbglevel, "get name Item=%s\n", item->item);
       rc = regexec(&preg, item->item, nmatch, pmatch,  0);
       if (rc == 0) {
          last_item = NULL;   /* keep this one */
    foreach_dlist(item, item_chain) {
       Dmsg2(dbglevel, "Got %s: %s\n", type, item->item);
       Mmsg(query, query2, item->item, jcr->pool->hdr.name);
-      Dmsg1(dbglevel, "query2=%s\n", query.c_str());
+      Dmsg1(dbglevel, "get id from name query2=%s\n", query.c_str());
       if (!db_sql_query(jcr->db, query.c_str(), dbid_handler, (void *)ids)) {
          Jmsg(jcr, M_FATAL, 0,
               _("SQL failed. ERR=%s\n"), db_strerror(jcr->db));
 
 
    if (rx.bsr->JobId) {
       uint32_t selected_files;
+      char ed1[50];
       if (!complete_bsr(ua, rx.bsr)) {   /* find Vol, SessId, SessTime from JobIds */
          bsendmsg(ua, _("Unable to construct a valid BSR. Cannot continue.\n"));
          goto bail_out;
          bsendmsg(ua, _("\n1 file selected to be restored.\n\n"));
       }
       else {
-         bsendmsg(ua, _("\n%u files selected to be restored.\n\n"), rx.selected_files);
+         bsendmsg(ua, _("\n%s files selected to be restored.\n\n"), 
+            edit_uint64_with_commas(rx.selected_files, ed1));
       }
    } else {
       bsendmsg(ua, _("No files selected to be restored.\n"));
 
    } else if (count == 1) {
       bsendmsg(ua, _("1 file unmarked.\n"));
    } else {
-      bsendmsg(ua, _("%d files unmarked.\n"), count);
+      char ed1[50];
+      bsendmsg(ua, _("%s files unmarked.\n"), edit_uint64_with_commas(count, ed1));
    }
    return 1;
 }
 
    " VolFirstWritten=%s VolParts=%u\n";
 static char Create_job_media[] = "CatReq Job=%s CreateJobMedia"
    " FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u"
-   " StartBlock=%u EndBlock=%u Copy=%d Strip=%d\n";
+   " StartBlock=%u EndBlock=%u Copy=%d Strip=%d MediaId=%s\n";
 static char FileAttributes[] = "UpdCat Job=%s FileAttributes ";
 static char Job_status[]     = "Status Job=%s JobStatus=%d\n";
 
-
-
 /* Responses received from the Director */
 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%lu"
    " VolBlocks=%lu VolBytes=%lld VolMounts=%lu VolErrors=%lu VolWrites=%lu"
    " MaxVolBytes=%lld VolCapacityBytes=%lld VolStatus=%20s"
    " Slot=%ld MaxVolJobs=%lu MaxVolFiles=%lu InChanger=%ld"
    " VolReadTime=%lld VolWriteTime=%lld EndFile=%lu EndBlock=%lu"
-   " VolParts=%lu LabelType=%ld";
+   " VolParts=%lu LabelType=%ld MediaId=%lld\n";
 
 
 static char OK_create[] = "1000 OK CreateJobMedia\n";
        return false;
     }
     memset(&vol, 0, sizeof(vol));
-    Dmsg1(110, "<dird %s", dir->msg);
+    Dmsg1(100, "<dird %s", dir->msg);
     n = sscanf(dir->msg, OK_media, vol.VolCatName,
                &vol.VolCatJobs, &vol.VolCatFiles,
                &vol.VolCatBlocks, &vol.VolCatBytes,
                &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
                &InChanger, &vol.VolReadTime, &vol.VolWriteTime,
                &vol.EndFile, &vol.EndBlock, &vol.VolCatParts,
-               &vol.LabelType);
-    if (n != 21) {
-       Dmsg2(110, "Bad response from Dir fields=%d: %s", n, dir->msg);
+               &vol.LabelType, &vol.VolMediaId);
+    if (n != 22) {
+       Dmsg2(100, "Bad response from Dir fields=%d: %s", n, dir->msg);
        Mmsg(jcr->errmsg, _("Error getting Volume info: %s"), dir->msg);
        return false;
     }
     bstrncpy(dcr->VolumeName, vol.VolCatName, sizeof(dcr->VolumeName));
     dcr->VolCatInfo = vol;            /* structure assignment */
 
-    Dmsg2(300, "do_reqest_vol_info return true slot=%d Volume=%s\n",
+    Dmsg2(100, "do_reqest_vol_info return true slot=%d Volume=%s\n",
           vol.Slot, vol.VolCatName);
     return true;
 }
 {
    JCR *jcr = dcr->jcr;
    BSOCK *dir = jcr->dir_bsock;
+   char ed1[50];
 
    /* If system job, do not update catalog */
    if (jcr->JobType == JT_SYSTEM) {
       dcr->VolFirstIndex, dcr->VolLastIndex,
       dcr->StartFile, dcr->EndFile,
       dcr->StartBlock, dcr->EndBlock, 
-      dcr->Copy, dcr->Stripe);
+      dcr->Copy, dcr->Stripe, 
+      edit_uint64(dcr->dev->VolCatInfo.VolMediaId, ed1));
     Dmsg1(100, ">dird: %s", dir->msg);
    if (bnet_recv(dir) <= 0) {
       Dmsg0(190, "create_jobmedia error bnet_recv\n");
 
    uint64_t VolCatCapacityBytes;      /* capacity estimate */
    uint64_t VolReadTime;              /* time spent reading */
    uint64_t VolWriteTime;             /* time spent writing this Volume */
+   int64_t VolMediaId;                /* MediaId */
    utime_t  VolFirstWritten;          /* Time of first write */
    bool     InChanger;                /* Set if vol in current magazine */
    char VolCatStatus[20];             /* Volume status */
 
               Technical notes on version 1.39  
 
 General:
+14Nov06
+kes  Implement code to pass the MediaId to the SD. The SD then uses
+     this MediaId when creating JobMedia records. This fixes a bug
+     with Migration where the MediaId got set to the second Volume
+     read rather than the write Volume.  Possibly (unlikely) fixes
+     bug #709.
+kes  Remove NextPool from Job resource.
+kes  Edit a few numbers in the restore dialog with commas.
+kes  Note, the DIR<->SD protocol has changed.
 13Nov06
 kes  Replace () by {} in configure.in for proper HP configuration.
 kes  Shorten some lines over 80 characters in filed/backup.c