#!/bin/bash

usage() {
    echo "Usage: $0 -i <ori_image_directory> -l <ori_label_directory> [-h]"
    echo "对image图片和label图片进行处理"
    echo "-i:原始图片的路径，-l:原始标签的路径，-h:帮助"
}

ori_image_directorys=""
ori_label_directorys=""

while getopts "hl:i:" opt; do
    case $opt in
        h)
            usage
            exit 0
            ;;
        i)
            ori_image_directorys=$OPTARG
            ;;
        l)
            ori_label_directorys=$OPTARG
            ;;
        *)
            echo  -e '\033[31m!!! Error, Illegal input !!!\033[0m'
            usage
            exit 1
            ;;
    esac
done

# 判断输入地址是否都为空
if [ -z "$ori_label_directorys" ] && [ -z "$ori_image_directorys" ]; then
    echo -e "\033[31m输入地址 -i -l 都为空\033[0m"
    usage
    exit 1
fi

# 地址转化
ori_image_directory=$(readlink -f "$ori_image_directorys")
ori_label_directory=$(readlink -f "$ori_label_directorys")
if [ -z "$ori_label_directory" ] && [ -z "$ori_image_directory" ]; then
    echo "无法解析地址，程序退出"
    echo -e "\033[31mori_image_directory\033[0m: $ori_image_directory"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    exit 1
fi
if [ ! -d "$ori_label_directory" ] && [ ! -d "$ori_image_directory" ]; then
    echo "image、label两目录都不存在，程序退出"
    echo -e "\033[31mori_image_directory\033[0m: $ori_image_directory"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    exit 1
fi

# 获取当前脚本的路径和名称
script_path=$(dirname "$0")
# 将当前目录更改为脚本所在的路径
cd "$script_path"

echo -e "\033[32m______ 1_rename_data.sh _____\033[0m"
while true; do
    PS3='Please enter your choice: '
    options=("Move all pics in dir to dir" "Delete all space in filename" "Add jpg to no suffix filename" "Replace content in filename" "Extract filename between prefix and suffix" "Add content before filename" "Add content behand filename" "Quit")
    echo "一般处理流程1(移动文件)、2(删除所有空格)、3(文件后缀添加.jpg)、4(删除内容)\"-恢复的\"/\"-副本\"、5(取出前缀、后缀中的内容)(方案1:Still/\"\"，方案2:\"\"/.Still)、6(添加前缀)、7(添加后缀)、8(关闭)"
    select opt in "${options[@]}"
    do
      case $opt in
        ### 选项1将所有在目录下的图片移动到目录中 ###
        "Move all pics in dir to dir")
            # 判断label图片路径是否存在
            if [ -d "$ori_label_directory" ]; then 
                echo "**** Processing ori_label_directory: $ori_label_directory ****"
                # 遍历 标签图片 目录中的所有后缀为 .png 或 .PNG 的文件，将其移动到主目录
                find "$ori_label_directory" -not -path "*/error/*" \( -iname "*.png" -o -iname "*.PNG" \) -type f -print0 |
                while IFS= read -r -d $'\0' file; do
                    echo cp -n "$file" "$ori_label_directory"
                    cp -n "$file" "$ori_label_directory"
                done
            fi
            echo ""
            # 判断image图片路径是否存在
            if [ -d "$ori_image_directory" ]; then 
                echo "**** Processing ori_image_directory: $ori_image_directory ****"
                # 遍历 原图片 目录中的所有后缀为 .png 或 .PNG 的文件，将其移动到主目录
                find "$ori_image_directory" -not -path "*/error/*" \( -iname "*.png" -o -iname "*.PNG" \) -type f -print0 |
                while IFS= read -r -d $'\0' file; do
                    echo cp -n "$file" "$ori_image_directory"
                    cp -n "$file" "$ori_image_directory"
                done
            fi
            echo -e ""
            break
            ;;
            
        ### 选项2替换文件名中的空格 ### 
        "Delete all space in filename")
          # 输入待删除的内容
          Del_str=" "
          Replace_str=""
          # 判断image图片路径是否存在
          if [ -d "$ori_image_directory" ]; then 
              echo "**** Processing ori_image_directory: $ori_image_directory ****"
              ls "$ori_image_directory" | grep -e "$Del_str" | awk -v ori_image_directory="$ori_image_directory" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(/ /, ""); print "mv \""ori_image_directory"/"s1"\" \""ori_image_directory"/"$1""Replace_str""$2"\""}'
              ls "$ori_image_directory" | grep -e "$Del_str" | awk -v ori_image_directory="$ori_image_directory" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(/ /, ""); print "mv \""ori_image_directory"/"s1"\" \""ori_image_directory"/"$1""Replace_str""$2"\""}' | bash
              echo -e ""
          else
              echo "**** image图片目录不存在: $ori_image_directory ****"
              echo -e ""
          fi
          
          # 判断label图片路径是否存在
          if [ -d "$ori_label_directory" ]; then 
              echo "**** Processing ori_label_directory: $ori_label_directory ****"
              ls "$ori_label_directory" | grep -e "$Del_str" | awk -v ori_label_directory="$ori_label_directory" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(/ /, ""); print "mv \""ori_label_directory"/"s1"\" \""ori_label_directory"/"$1""Replace_str""$2"\""}'
              ls "$ori_label_directory" | grep -e "$Del_str" | awk -v ori_label_directory="$ori_label_directory" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(/ /, ""); print "mv \""ori_label_directory"/"s1"\" \""ori_label_directory"/"$1""Replace_str""$2"\""}' | bash
              echo -e ""
          else
              echo "**** label图片目录不存在: $ori_label_directory ****"
              echo -e ""
          fi
          break
          ;;
        
        ### 选项3在无后缀文件后添加.jpg后缀 ### 
        "Add jpg to no suffix filename")
          # 判断image图片路径是否存在
          if [ -d "$ori_image_directory" ]; then 
              echo "**** Processing ori_image_directory: $ori_image_directory ****"
              find "$ori_image_directory" -type f ! -name "*.*" | awk -F/ '{print $NF}' | awk -v ori_image_directory="$ori_image_directory" '{print "mv \""ori_image_directory"/"$0"\" \""ori_image_directory"/"$0".jpg\""}'
              find "$ori_image_directory" -type f ! -name "*.*" | awk -F/ '{print $NF}' | awk -v ori_image_directory="$ori_image_directory" '{print "mv \""ori_image_directory"/"$0"\" \""ori_image_directory"/"$0".jpg\""}' | bash
              echo -e ""
          else
              echo "**** image图片目录不存在: $ori_image_directory ****"
              echo -e ""
          fi
          
          # 判断label图片路径是否存在
          if [ -d "$ori_label_directory" ]; then 
              echo "**** Processing ori_label_directory: $ori_label_directory ****"
              find "$ori_label_directory" -type f ! -name "*.*" | awk -F/ '{print $NF}' | awk -v ori_label_directory="$ori_label_directory" '{print "mv \""ori_label_directory"/"$0"\" \""ori_label_directory"/"$0".jpg\""}'
              find "$ori_label_directory" -type f ! -name "*.*" | awk -F/ '{print $NF}' | awk -v ori_label_directory="$ori_label_directory" '{print "mv \""ori_label_directory"/"$0"\" \""ori_label_directory"/"$0".jpg\""}' | bash
              echo -e ""
          else
              echo "**** label图片目录不存在: $ori_label_directory ****"
              echo -e ""
          fi
          break
          ;;

        ### 选项4替换文件名中内容 ### 
        "Replace content in filename")
          # 输入待删除的内容
          echo -n "Please input the content to be deleted = "
          read -r Del_str
          echo -n "Please input the content to be replace(default is None) = "
          read -r Replace_str
          # 判断image图片路径是否存在
          if [ -d "$ori_image_directory" ]; then 
              echo "**** Processing ori_image_directory: $ori_image_directory ****"
              ls "$ori_image_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | grep -e "$Del_str" | awk -v ori_image_directory="$ori_image_directory" -v Del_str="$Del_str" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(Del_str, Replace_str); print "mv \""ori_image_directory"/"s1"\" \""ori_image_directory"/"$0"\""}'
              ls "$ori_image_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | grep -e "$Del_str" | awk -v ori_image_directory="$ori_image_directory" -v Del_str="$Del_str" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(Del_str, Replace_str); print "mv \""ori_image_directory"/"s1"\" \""ori_image_directory"/"$0"\""}' | bash
              echo -e ""
          else
              echo "**** image图片目录不存在: $ori_image_directory ****"
              echo -e ""
          fi
          
          # 判断label图片路径是否存在
          if [ -d "$ori_label_directory" ]; then 
              echo "**** Processing ori_label_directory: $ori_label_directory ****"
              ls "$ori_label_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | grep -e "$Del_str" | awk -v ori_label_directory="$ori_label_directory" -v Del_str="$Del_str" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(Del_str, Replace_str); print "mv \""ori_label_directory"/"s1"\" \""ori_label_directory"/"$0"\""}'
              ls "$ori_label_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | grep -e "$Del_str" | awk -v ori_label_directory="$ori_label_directory" -v Del_str="$Del_str" -v Replace_str="$Replace_str" -F "$Del_str" '{s1=$0; gsub(Del_str, Replace_str); print "mv \""ori_label_directory"/"s1"\" \""ori_label_directory"/"$0"\""}' | bash
              echo -e ""
          else
              echo "**** label图片目录不存在: $ori_label_directory ****"
              echo -e ""
          fi
          break
          ;;
          
        ### 选项5删除文件名前缀、后缀之间的内容 ###
        "Extract filename between prefix and suffix")
          # 输入待删除的内容
          echo -n "Please input the prefix to be deleted = "
          read -r prefix
          echo -n "Please input the suffix to be deleted = "
          read -r suffix

           
          # 判断image图片路径是否存在
          if [ -d "$ori_image_directory" ]; then 
                echo "**** Processing ori_image_directory: $ori_image_directory ****"
                # ls -1 以回车显示
                ls -1 "$ori_image_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | grep -e "$prefix" | grep -e "$suffix" | while read file_name; do
                    # 提取出新的文件名
                    if [ -z $prefix ];then
                        file_name_new=$(echo $file_name | sed "s/\(.*\)$suffix.*\(\.jpg\|\.png\|\.bmp\|\.JPG\|\.PNG\|\.BMP\)$/\1\2/")
                    else
                        echo "sed "s/^.*$prefix\(.*\)$suffix.*\(\.jpg\|\.png\|\.bmp\|\.JPG\|\.PNG\|\.BMP\)$/\1\2/""
                        file_name_new=$(echo $file_name | sed "s/^.*$prefix\(.*\)$suffix.*\(\.jpg\|\.png\|\.bmp\|\.JPG\|\.PNG\|\.BMP\)$/\1\2/")
                    fi
                    echo "$file_name -> $file_name_new"
                    echo "mv "$ori_image_directory/$file_name" "$ori_image_directory/$file_name_new""
                    mv "$ori_image_directory/$file_name" "$ori_image_directory/$file_name_new"
                done
                # ls "$ori_image_directory" | grep -e "$Del_str" | awk -v ori_image_directory="$ori_image_directory" -v Del_str="$Del_str" '{ s1=$0; match($0, Del_str); sub(substr($0, 1, RSTART + RLENGTH - 1), ""); print "mv \""ori_image_directory"/"s1"\" \""ori_image_directory"/"$0"\""}'
                # ls "$ori_image_directory" | grep -e "$Del_str" | awk -v ori_image_directory="$ori_image_directory" -v Del_str="$Del_str" '{ s1=$0; match($0, Del_str); sub(substr($0, 1, RSTART + RLENGTH - 1), ""); print "mv \""ori_image_directory"/"s1"\" \""ori_image_directory"/"$0"\""}' | bash
                echo -e ""
          else
                echo "**** image图片目录不存在: $ori_image_directory ****"
                echo -e ""
          fi
          
          # 判断label图片路径是否存在
          if [ -d "$ori_label_directory" ]; then 
                echo "**** Processing ori_label_directory: $ori_label_directory ****"
                ls -1 "$ori_label_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | grep -e "$prefix" | grep -e "$suffix" | while read file_name; do
                    # 提取出新的文件名
                    if [ -z $prefix ];then
                        file_name_new=$(echo $file_name | sed "s/\(.*\)$suffix.*\(\.jpg\|\.png\|\.bmp\|\.JPG\|\.PNG\|\.BMP\)$/\1\2/")
                    else
                        echo "sed "s/^.*$prefix\(.*\)$suffix.*\(\.jpg\|\.png\|\.bmp\|\.JPG\|\.PNG\|\.BMP\)$/\1\2/""
                        file_name_new=$(echo $file_name | sed "s/^.*$prefix\(.*\)$suffix.*\(\.jpg\|\.png\|\.bmp\|\.JPG\|\.PNG\|\.BMP\)$/\1\2/")
                    fi
                    echo "$file_name -> $file_name_new"
                    echo "mv "$ori_label_directory/$file_name" "$ori_label_directory/$file_name_new""
                    mv "$ori_label_directory/$file_name" "$ori_label_directory/$file_name_new"
                done
              # ls "$ori_label_directory" | grep -e "$Del_str" | awk -v ori_label_directory="$ori_label_directory" -v Del_str="$Del_str" '{ s1=$0; match($0, Del_str); sub(substr($0, 1, RSTART + RLENGTH - 1), ""); print "mv \""ori_label_directory"/"s1"\" \""ori_label_directory"/"$0"\""}'
              # ls "$ori_label_directory" | grep -e "$Del_str" | awk -v ori_label_directory="$ori_label_directory" -v Del_str="$Del_str" '{ s1=$0; match($0, Del_str); sub(substr($0, 1, RSTART + RLENGTH - 1), ""); print "mv \""ori_label_directory"/"s1"\" \""ori_label_directory"/"$0"\""}' | bash
              echo -e ""
          else
              echo "**** label图片目录不存在: $ori_label_directory ****"
              echo -e ""
          fi
          break
          ;;

        ### 选项6在文件名前添加内容 ### 
        "Add content before filename")
          echo -n "Please input the content to be added = "
          read Group_str
          # 判断image图片路径是否存在
          if [ -d "$ori_image_directory" ]; then 
              echo "**** Processing ori_image_directory: $ori_image_directory ****"
              ls "$ori_image_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_image_directory="$ori_image_directory"  -v Group_str="$Group_str" '{print "mv \""ori_image_directory"/"$0"\" \""ori_image_directory"/"Group_str""$0"\""}'
              ls "$ori_image_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_image_directory="$ori_image_directory"  -v Group_str="$Group_str" '{print "mv \""ori_image_directory"/"$0"\" \""ori_image_directory"/"Group_str""$0"\""}' | bash
              echo -e ""
          else
              echo "**** image图片目录不存在: $ori_image_directory ****"
              echo -e ""
          fi
          
          # 判断label图片路径是否存在
          if [ -d "$ori_label_directory" ]; then 
              echo "**** Processing ori_label_directory: $ori_label_directory ****"
              ls "$ori_label_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_label_directory="$ori_label_directory"  -v Group_str="$Group_str" '{print "mv \""ori_label_directory"/"$0"\" \""ori_label_directory"/"Group_str""$0"\""}'
              ls "$ori_label_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_label_directory="$ori_label_directory"  -v Group_str="$Group_str" '{print "mv \""ori_label_directory"/"$0"\" \""ori_label_directory"/"Group_str""$0"\""}' | bash
              echo -e ""
          else
              echo "**** label图片目录不存在: $ori_label_directory ****"
              echo -e ""
          fi
          break
          ;;
          
        ### 选项7在文件名后添加内容 ### 
        "Add content behand filename")
          echo -n "Please input the content to be added = "
          read Group_str
          # 判断image图片路径是否存在
          if [ -d "$ori_image_directory" ]; then 
              echo "**** Processing ori_image_directory: $ori_image_directory ****"
              ls "$ori_image_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_image_directory="$ori_image_directory"  -v Group_str="$Group_str" -F . '{print "mv \""ori_image_directory"/"$0"\" \""ori_image_directory"/"substr($0, 1, length($0)-length($NF)-1)""Group_str"."$NF"\""}'
              ls "$ori_image_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_image_directory="$ori_image_directory"  -v Group_str="$Group_str" -F . '{print "mv \""ori_image_directory"/"$0"\" \""ori_image_directory"/"substr($0, 1, length($0)-length($NF)-1)""Group_str"."$NF"\""}' | bash
              echo -e ""
          else
              echo "**** image图片目录不存在: $ori_image_directory ****"
              echo -e ""
          fi
          
          # 判断label图片路径是否存在
          if [ -d "$ori_label_directory" ]; then 
              echo "**** Processing ori_label_directory: $ori_label_directory ****"
              ls "$ori_label_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_label_directory="$ori_label_directory"  -v Group_str="$Group_str" -F . '{print "mv \""ori_label_directory"/"$0"\" \""ori_label_directory"/"substr($0, 1, length($0)-length($NF)-1)""Group_str"."$NF"\""}' 
              ls "$ori_label_directory" | grep -E "\.(png|jpg|PNG|JPG|BMP|bmp)$" | awk -v ori_label_directory="$ori_label_directory"  -v Group_str="$Group_str" -F . '{print "mv \""ori_label_directory"/"$0"\" \""ori_label_directory"/"substr($0, 1, length($0)-length($NF)-1)""Group_str"."$NF"\""}' | bash
              echo -e ""
          else
              echo "**** label图片目录不存在: $ori_label_directory ****"
              echo -e ""
          fi
          break
          ;;

        ### 选项8 退出 ### 
        "Quit")
            echo "Exiting..."
            exit 0
            ;;
          
        *)
            echo "Invalid option: $REPLY"
            echo -e ""
            break
            ;;
      esac
    done
done
