#!/bin/bash 

usb_config=/etc/usbcopyauto
if [ -z "$1" ]; then
    echo "usage: $0 [mntpath]"
    exit 1
fi
mntpath=$1
device_name=`df | grep "$mntpath$" | awk '{print $1}'`
if [ -z "$device_name" ]; then
    echo "usage: $0 [mntpath]"
    exit 1
fi

COPY_UUID=`iniparse -c -f "${usb_config}" -s usb -k uuid`
DEV_UUID=`blkid -o export ${device_name} | awk -F= '/^UUID=/ {print $2}'`
if [ ! -z "${COPY_UUID}" -a "${COPY_UUID}" = "${DEV_UUID}" ]; then
    local flag=`iniparse -c -f "${usb_config}" -s usb -k "local"`
    local sharename=$(basename ${mntpath})
    local configFile=/etc/bakuplist
    [ "$flag" = "1" ] && configFile=/etc/bakup2nas
    local tmp=`cat $configFile | grep ${sharename}`
    [ "${tmp}" = "" ] && sed -i "s/usbshare[0-9]/${sharename}/g" $configFile
    local COPY_DIRS=`iniparse -c -f "${usb_config}" -s usb -k target`
    local LOG_FILE=`iniparse -c -f "${usb_config}" -s usb -k logfile`
    [ "$flag" = "1" ] && {
        /usr/sbin/ter_cp -l ${LOG_FILE} -f ${mntpath} -t ${COPY_DIRS} -m cp -o 1 &
    }||{
        /usr/sbin/ter_cp -l ${LOG_FILE} -f ${COPY_DIRS} -t ${mntpath} -m cp -o 1 &
    }
fi

exit 0
