#!/bin/bash

path=/var/log/wiwo
if [[ $1 == 1 ]]; then
    path=/var/log/wiwo
elif [[ $1 == 2 ]]; then
    path=/data/log/wiwo
fi
filelist=$(ls $path/)
for line in $filelist
do
    arr=(${line//#/ })
    if [[ ${arr[0]}&&${arr[1]}&&${arr[2]} ]]
    then
        filename=${arr[0]}
        timestamp=${arr[1]}
        tmpext=${arr[2]}
        flag=(${tmpext//./ })
        if [[ ${flag[0]} == 'tmp' ]]
        then
            timestamp=$(date -d @$timestamp "+%Y%m%d-%H:%M:%S")
            mv $path/$line $path/$filename-$timestamp.${flag[1]}
        fi
    fi
done

for line in $filelist
do
    arr=(${line//log-/ })
    filedate=(${arr[1]//-/ })
    datestr=$(date -d "-1 day" "+%Y%m%d")
    if [[ "$filedate" == "" ]];
    then
        continue
    fi
    if [[ "$filedate" < "$datestr" ]];
    then
        rm -f $path/$line
    fi
done
