#!/bin/bash

# rar
# find . -name "*.rar" -printf "%f\n" > file_list.tmp

# 7z
# find . -name "*.7z" -printf "%f\n" > file_list.tmp

sed -i -e 's/^/"/g' -e 's/$/"/g' file_list.tmp

mkdir origin/ converted/
function run_convert(){
    mkdir tmp1/
    f_in=[email protected]

    # rar
    # f_out=$(echo ${f_in} | sed 's/\.rar/\.zip/g')

    # 7z
    # f_out=$(echo ${f_in} | sed 's/\.rar/\.zip/g')

    # rar 无密码
    # unrar x "${f_in}" tmp1

    # rar 有密码
    # password="password"
    # unrar x -p${password} "${f_in}" tmp1

    # 7z 无密码
    # 7z x -otmp1 "${f_in}"

    # 7z 有密码
    # password="password"
    # 7z x -otmp1 -p${password} "${f_in}"

    mkdir tmp2/
    find tmp1/ -name "*.png" -exec mv {} tmp2/ \;
    find tmp1/ -name "*.jpg" -exec mv {} tmp2/ \;
    find tmp1/ -name "*.jpeg" -exec mv {} tmp2/ \;
    find tmp1/ -name "*.gif" -exec mv {} tmp2/ \;
    cd tmp2/
    zip -r "../converted/${f_out}" *
    cd ../
    rm -r tmp1 tmp2
    mv "${f_in}" origin/
}

export -f run_convert
xargs -a file_list.tmp -n 1 bash -c 'run_convert "[email protected]"' _
rm file_list.tmp