#/bin/bash -e

#########################帮助文件#################################
help() {
    cat <<EOF
用法：$0 [-h|--help] [-i|--in] [-o|out] [-t|-tmpdir] [-s|--scan] path
-h|--help     显示这个帮助
-i|--in       输入的文件路径
-o|--out      输出的文件路径，默认为运行目录
-t|--tmpdir   设置解包路径，默认为运行目录
-s|--scan     进入扫描模式，扫描这个路径下的所有deb包。在此模式下，输出文件路径将变为输入文件所在目录

如果没有设置参数，将按照 [输入路径] [输出路径] [解包路径] 的顺序读取
--------------------------------------------------------------------
Usage: $0 [-h|--help] [-i|--in] [-o|out] [-t|-tmpdir] [-s|--scan] path
-h|--help     Show this text
-i|--in       The input file path
-o|--out      The ourput file directory, defalut is current dir
-t|--tmpdir   The unpack directory, defalut is current dir
-s|--scan     Enter scan mode.Scan all the package in the given path.
              If you are under scan mode, output will become the input file directory

If no args detected, will read in the order of [input path] [ output dir ] [ unpack dir ].
--------------------------------------------------------------------
例子 Examples：
$0 -i ./spark-store_3.0.3-13_amd64.deb -o ./ -t ./
$0 ./spark-store_3.0.3-13_amd64.deb ./ ./
$0 ./spark-store_3.0.3-13_amd64.deb
$0 --scan ./

EOF
}
#########################帮助文件结束#############################

#########################检测参数功能#############################
parse_args() {
    while [ $# -gt 0 ]; do
        case "$1" in
        -h|--help)
            help
            exit
            ;;
        -i|--in)
            DEBPATH="$2"
            shift
            ;;
		-o|--out)
			OUTPATH="$2"
			shift
			;;
		-t|--tmpdir)
			TMPDIR="$2"
			shift
			;;
		-s|--scan)
			isscan="1"
			SCANDIR="$2"
			break
			;;
        *)
            DEBPATH="$1"
			OUTPATH="$2"
			TMPDIR="$3"
			break
			#没有参数就读完退出
            ;;
    esac
    shift
    done
}

#########################检测参数结束#############################
#以上参考自abcfy2的脚本




echo "repack-zstd script. Copyright The Spark Project 2022-Now"
echo
##########初始化变量
DEBPATH=""
OUTPATH=""
TMPDIR=""

##########调用参数分析，读入参数
parse_args "$@"


##########检测参数是否有效
if [ "$isscan" = "1" ];then
########如果是scan就跳出
echo "进入扫描模式"
if [ "$SCANDIR" = "" ];then
SCANDIR="."
fi

files=(`find "$SCANDIR" -name '*.deb'`)
until [ "${#files[@]}" = "0" ];do
filepath=${files[0]}
filedir=`echo $filepath | xargs -I {} dirname {}`
files=(${files[@]:1})
$0 $filepath $filedir
done
exit

fi

if [ "$1" = "" ];then
	echo "没有任何输入，退出"
	echo "可使用$0 -h 来查看帮助"
    echo "No input detected. Exit"
    echo "Use $0 -h to get help"
	exit
fi

if [ "$DEBPATH" = "" ];then
	echo "没有检测到输入路径，退出"
	echo "可使用$0 -h 来查看帮助"
    echo "No input path detected. Exit"
    echo "Use $0 -h to get help"
	exit
fi

if [ ! -e "$DEBPATH" ];then
	echo "错误：输入的位置 $DEBPATH 无效"
    echo "E: Invalid input file path"
	exit
fi

echo "正在处理位于$DEBPATH的文件"

if [ "$OUTPATH" = "" ];then
	echo "没有检测到输出目录，默认使用运行目录"
    echo "W: No output path detected. Use current directory as default"
	OUTPATH=`pwd`
fi

if [ "$TMPDIR" = "" ];then
	echo "没有检测到解包目录，默认使用运行目录"
    echo "W: No unpack directory detected. Use current directory as default"
	TMPDIR=`pwd`
fi

if [ ! -d "$OUTPATH" ];then
	echo "错误：输出目录无效，退出"
    echo "E: Invalid output path. Exit"
	exit 
fi

if [ ! -d "$TMPDIR" ];then
	echo "错误：解包目录无效，退出"
    echo "E: Invalid unpack directory. Exit"
	exit 
fi
##################################################
# 检测文件类型


iszstd=`file "$DEBPATH" | grep tar.zs`
#iszstd=1

##################################################
#判断是否为zstd并执行相关操作

if [ "$iszstd" != "" ];then
	echo "这是zstd包！解压并重新打包"
    echo "This is debian package with zstd! Unpack and repack..."
	mkdir -p "$TMPDIR/unpack-dir"
	dpkg -X "$DEBPATH" "$TMPDIR/unpack-dir" 
	dpkg -e "$DEBPATH" "$TMPDIR/unpack-dir/DEBIAN"
	dpkg-deb -Z xz  -b "$TMPDIR/unpack-dir/" "$OUTPATH"
	echo 重打包已完成，删除tmp
    echo "Repack finished. Remove tmp dir"
	rm -rf "$TMPDIR/unpack-dir"
	exit
	
else
	echo "这不是zstd包！移动到输出位置"
    echo "This is NOT debian package with zstd ! Move the file to output path..."
	mv -n "$DEBPATH" "$OUTPATH"
fi

echo "---------------------------------------------------------------------------"
