#!/bin/csh

# Set the directory to be processed (default is the current directory)
set case_dir = "../../case/config"

# If a directory is provided as a parameter, use that directory.
#if ($#argv > 0) then
#    set case_dir = $argv
#endif

# Check if two directories and a log file are provided as parameters.
if ( $#argv != 1 ) then
    echo "Usage: $0 SIM"
    exit 1
endif

set SIM = $1

# Initialize Counter
set dir_count = 1

# List all directories, and print their sequence numbers.
echo "Listing directories in $case_dir :"
foreach dir (`ls -d $case_dir/*/`)
#    set filelist = `basename $file .txt`
    echo "$dir_count : $dir"    
#    echo "$count : $filelist"
    @ dir_count++
end


# Obtain the sequence number input by the user from the command line.
echo -n "Enter the start number: "
set start = $<
echo -n "Enter the end number: "
set end = $<
echo -n "Whether save sim files? 1: save; 0: delete."
echo -n "Enter your choice: "
set save = $<
if ("$save" == "") then
    set save = 0
endif

make -s comp WAVE=$save

if ( $start < 1 || $end > $dir_count - 1 || $start > $end ) then
    echo "Invalid directory range. Please enter a valid range."
    exit 1
endif

set selected_dirs = ()
@ dir_index = 1
foreach dir (`ls -d $case_dir/*/`)
    if ( $dir_index >= $start && $dir_index <= $end ) then
        set selected_dirs = ($selected_dirs $dir)        
    endif
    @ dir_index++
end

foreach folder ($selected_dirs)
    echo "Processing folder: $folder"

    # 获取文件夹中的所有 TXT 文件
    set file_list = ($folder/*.txt)
    set txt_count = $#file_list
    # 产生对应的文件夹地址
    set gather_folder   = `echo $folder | sed 's|/config/|/gather/|'`
    set result_folder   = `echo $folder | sed 's|../../case/config/|result/|g'`    
    set coverage_folder = `echo $folder | sed 's|../../case/config/|coverage/|g'`    

    # 打印 result_folder 的值
    echo "Result folder: $result_folder"
    mkdir -p $gather_folder
    # 检查是否有 TXT 文件
    if ( $#file_list == 0 ) then
        echo "No .txt files found in $folder. Skipping..."
        continue
    endif
    mkdir -p $result_folder
    # TB从这些文件中读取case,result等文件的路径
    set temp_files = ("case_temp.txt" "data_temp.txt" "result_temp.txt")
    # 遍历文件列表，检查是否存在，不存在则创建，这样git就不用追踪这些txt文件了
    foreach file ($temp_files)
        if ( ! -e $file ) then
            touch $file
        endif
    end
    #遍历文件夹中的所有 TXT 文件
    @ i = 1
    foreach file ($file_list)
        # echo "Processing file: $file"
         # set file_list = ($case_dir/*.txt)

        set rstdir = "./data_$SIM"
        mkdir -p $rstdir

        while ( $i <= $txt_count )
          set filename = `basename $file_list[$i] .txt`
          set filen    = `basename $file_list[$i]`
          set dirname  = `dirname  $file_list[$i]`
          # 拼接目标文件路径
          set gather_file = "$gather_folder/$filename.txt"
          set result_file = "$result_folder/$filename.txt"
          # 创建result等相关文件
          set file_lists = ($result_file)
          # 遍历文件列表，检查并创建文件
          foreach file ($file_lists)
              touch $file
              echo "" > $file
              # 输出创建文件的状态
              if ( $status == 0 ) then
                  echo "File $file created successfully."
              else
                  echo "Failed to create file $file."
              endif
          end
          # Create directories corresponding to filenames
          set newdir = "./work_$SIM/$filename"
          mkdir -p $newdir
          # echo $status
          if ( $status == 0 ) then
            echo "Directory $newdir created successfully."
          else
            echo "Failed to create directory $newdir."
          endif
          # echo "Directory $newdir created."
          # Copy the simv file to the corresponding folder.
          echo "Copy the simv file to the $newdir folder."
          \cp -f ./simv $newdir/
          \cp -rf simv.daidir $newdir/

          \cp -f ./backup/Makefile $newdir/
          \cp -f ./backup/filelist_syn.f $newdir/
          \cp -f ./backup/filelist_vlg.f $newdir/

          echo ""  > ./case_temp.txt
          echo ""  > ./data_temp.txt
          echo ""  > ./result_temp.txt
          echo " ../../$file_list[$i]"    >> case_temp.txt
          echo " ../../${result_file}"    >> result_temp.txt
          set result = "data_$SIM"
             
          set new_dir = `echo "$dirname" | sed 's|../../case/config|'$result'|'`
          mkdir -p $new_dir
          set result = "$new_dir/$filen"
          touch $result
      
          echo "../../$result" >> data_temp.txt          
          cd $newdir/
          # Run "run" command in makefile
          ./simv -l sim.log -cm line+cond+fsm+tgl+branch  -cm_dir ../../$coverage_folder -cm_name $filename "+ENABLE_FSDB=$save"          
          cd ../../
          if ( $save == 0 ) then
            rm -rf $newdir
          endif
          @ i++                       
        end
    end
end





 

