#!/bin/bash

sde2=$(df |grep sde2|awk '{print $1}')

if [ ! ${sde2} ];then
   echo "sde error"
   exit
else
   echo "sde success"
fi
work=/opt/collect/20-eth0.network
work1=/opt/collect/21-eth1.network
#ls -l /opt/collect/20-eth0.network >/dev/null 2>&1
#if [ $? -eq 0 ];then
if [  -f ${work} ] && [ -f ${work1} ];then
   echo "fileexist success"
elif [ -L "/etc/systemd/network/*.network" ];then
   echo "profile error"
   exit
else
   cp /etc/systemd/network/*.network /opt/collect/
   echo "profile success"
fi

if [ $# -eq 2 ]
then
        case "$1" in
        -path)
                file=$2
                cd ${file%/*}
                if `tar -zxmf ${file##*/}`
                then
                        echo "tar $2 extract success"
                        umount /dev/mmcblk1p11
                        umount /dev/mmcblk1p12
                        if `manage -update ./package ./rootfs.tar.gz`
                        then
                                echo "system update success"
                                rm -rf ./package ./rootfs.tar.gz
                                sync
                                fsck -y /dev/mmcblk1p11
                                fsck -y /dev/mmcblk1p12
                                sync
                                sync
                                sync
                                reboot
                        else
                                echo "system update error"
                        fi

                else
                        echo "tar extract error"
                fi;;

        *) echo "option error";;
        esac
        shift
else
        echo "paramters number error!!!"
        echo "Usage:saturn-system-update.sh -path xxx.tar.gz"
fi
