#!/bin/ksh
imagedir="/export/home1/sdc_image_pool/images"
StudyID="0020,0010"
PatientName="0010,0010"
StudyDate="0008,0020"
StudyTime="0008,0030"
here=$PWD
cd $imagedir
exam=`find . -name "e*"`
for edir in $exam
do
    sdir=`ls -1 $edir | head -1`
    f1=`ls $edir/$sdir/*.1`
    line=`$here/dcmdump $f1 +P $StudyID +E`
    id=`echo $line | tr '[' '\011' | tr ']' '\011' | cut -f2`
    line=`$here/dcmdump $f1 +P $PatientName +E`
# quote $line variable to keep all spaces
    name=`echo "$line" | tr '[' '\011' | tr ']' '\011' | tr ' ' '_' | cut -f2`
    if (test ${#name} -eq ${#line})
        then
        name='____'
    fi
    ename=`echo $edir | cut -c3-`
    line=`$here/dcmdump $f1 +P $StudyDate +E`
    studydate=`echo $line | tr '[' '\011' | tr ']' '\011' | cut -f2`

    typeset -L4 year=$studydate
    typeset -R4 month_day=$studydate
    typeset -L2 month=$month_day
    typeset -R2 day=$month_day
    line=`$here/dcmdump $f1 +P $StudyTime +E`
    studytime=`echo $line | tr '[' '\011' | tr ']' '\011' | cut -f2`
    typeset -L2 hour=$studytime
    typeset -R4 min_sec=$studytime
    typeset -L2 min=$min_sec
    typeset -R2 sec=$min_sec
    date1=`echo $month $day $year | awk '{printf "%02d/%02d/%04d", $1, $2, $3}'`
    time1=`echo $hour $min $sec | awk '{printf "%02d:%02d:%02d", $1, $2, $3}'`
    echo $studydate $studytime $date1 $time1 $id $name $ename | awk '{printf "%s%s %s %s %05ld %-43.43s %-43.43s\n", $1, $2, $3, $4, $5, $6, $7}'
done
cd $here
