#!/bin/bash

usage() {
    echo "Usage: $0 -i <ori_image_directory> -l <ori_label_directory> -r <stack_result_directory> [ -a <alpha> -p <prefix> -s <suffix> -h]"
    echo "对image图片和label图片进行匹配（-i、-l -r均不能为空）（-p -s默认为空"" -a默认为\"0.3\") "
    echo "-i:原始image的路径，-l:原始label的路径，-p:前缀内容，-s:后缀内容(不用管文件后缀名)，-h:帮助"
    echo "e.g. 5_TOOL_stack_pics.sh -i ./C组未标注 -l ./C组标注图片 -r ./C组result_0.3透明度 -a 0.3 -p Group_C_ -s _label" 
}

ori_image_directorys=""
ori_label_directorys=""
stack_result_directorys=""
prefix=""
suffix=""
alpha="0.3"

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

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

# 地址转化
ori_image_directory=$(readlink -f "$ori_image_directorys")
ori_label_directory=$(readlink -f "$ori_label_directorys")
stack_result_directory=$(readlink -f "$stack_result_directorys")
if [ -z "$ori_label_directory" ] || [ -z "$ori_image_directory" ]|| [ -z "$stack_result_directory" ]; then
    echo "image、label、result存在无法解析地址，程序退出"
    echo -e "\033[31mori_image_directory\033[0m: $ori_image_directorys"
    echo -e "\033[31mori_label_directory\033[0m: $ori_label_directorys"
    echo -e "\033[31mori_label_directory\033[0m: $stack_result_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"

# 激活conda环境
source /home/"$USER"/miniconda/bin/activate Deal_pics

echo -e "\033[32m_____ 5_TOOL_stack_pics.sh _____\033[0m"
echo -e "\033[33mimage所在文件夹为$ori_image_directory\nlable所在文件夹为$ori_label_directory\033[0m"
# 遍历label目录
for file_path in "$ori_label_directory"/*; do
    # 判断是否是文件
    if [[ -f "$file_path" ]]; then
        file_name=$(basename "$file_path")
        # 判断文件名是否符合规范
        if [[ "$file_name" =~ .*\.(jpg|png|bmp|JPG|PNG|BMP) ]]; then # 判断是否有为图片
        # if [[ "$file_name" =~ "$prefix".*"$suffix".*\.(jpg|png|bmp|JPG|PNG|BMP)$ ]]; then # 判断是否有满足要求的文件名
            # 抽取文件名（有前缀、后缀的抽取前缀、后缀里面的，没有的返回整个）
            if [ -z $prefix ];then
                file_name_extract=$(echo $file_name | sed "s/"$prefix"\(.*\)"$suffix".*/\1/" | sed "s/\(.*\)\.\(jpg\|png\|bmp\|JPG\|PNG\|BMP\)$/\1/")
            else
                file_name_extract=$(echo $file_name | sed "s/".*$prefix"\(.*\)"$suffix".*/\1/" | sed "s/\(.*\)\.\(jpg\|png\|bmp\|JPG\|PNG\|BMP\)$/\1/")
            fi
            # 从label目录中看是否有此文件
            file_name_other=$(ls $ori_image_directory | grep $file_name_extract)
            file_name_other=$(echo "$(echo "$file_name_other" | sed '/^$/d')" | head -n1) # 提取出文件名
            # 如果另一个目录没有此文件的话
            if [ -z "$file_name_other" ]; then
                echo "$file_name label中对应内容未在$ori_image_directory搜索到"
                # 建立相关存储文件夹
                if [ ! -d "$ori_label_directory/Not_pair_pics" ]; then
                    mkdir -p "$ori_label_directory/Not_pair_pics" # 建立存储文件夹
                fi
                # 移动相关文件
                cp "$ori_label_directory/$file_name" "$ori_label_directory/Not_pair_pics"
                echo "$file_name" >> "$ori_label_directory/Not_pair_pics/not_pair.txt"
            else # 如果另一个目录有此配对文件的话，则运行相关程序
                echo "image中的$file_name_other，与lable中的$file_name"
                mkdir -p "$stack_result_directory"
                python 5_stack_picture.py "$ori_image_directory/$file_name_other" "$ori_label_directory/$file_name" "$stack_result_directory" "$alpha"
                echo ""
            fi
            
        fi
    else
        echo "$file_path不是文件"
    fi
done
