#!/bin/bash
#scvfirefoxmigrate 2018/10/02
#Copyright J's Communication Co., Ltd.
#========================================
PROGNAME="scvffmigrate"
PROGVER="1.0.3"
SCVCONF="/var/www/html/scv/.scvconf"
DBPASS=`cat $SCVCONF | grep "DBPWD" | sed -e 's@SCV_DBPWD=@@g' | tr -d "'"`

#----------------------------------------
usage(){
cat << EOF
$PROGNAME $PROGVER
This program migrates firefoxprofile.
Usage:
  $PROGNAME -migrate     #Migrate firefoxprofile
  $PROGNAME -restore     #Restore migrated firefoxprofile
  $PROGNAME -v           #show verion of this
EOF

}

#----------------------------------------
sure() {
  echo "Are you sure to continue? [y/N]"
  read ANSWER
  case $ANSWER in
    "Y" | "y" | "yes" | "Yes" | "YES" )  : ;;
    * ) echo "command canceled!"; exit 0 ;;
  esac
}

#----------------------------------------
encryption() {
  PROFDIR="$1"
  if [[ -e "$PROFDIR" ]]; then
    echo "migration to $PROFDIR from $PROFDIR.zip"
    CDDIR=`echo "$PROFDIR" | sed -e "s@firefoxprof@@g"` 
    cd $CDDIR
    zip -re --password="$DBPASS" firefoxprof.zip firefoxprof
    check="$?"
    if [ $check -eq 0 ];then
      echo "Migration complete $PROFDIR.zip."
      rm -rf $PROFDIR
    else
      echo "Migration failed!"
    fi
  echo "----------------------------------------"
  fi
}

composite() {
  PROFDIR="$1"
  if [[ -e "$PROFDIR" ]]; then
    echo "composite to $PROFDIR from `echo $PROFDIR | sed -e 's@.zip@@g'`"
    CDDIR=`echo "$PROFDIR" | sed -e "s@firefoxprof.zip@@g"`
    cd $CDDIR
    unzip -oP "$DBPASS" firefoxprof.zip
    check="$?"
    if [ $check -eq 0 ];then
      echo "Composite complete $PROFDIR."
      rm -rf $PROFDIR
    else
      echo "Composite failed!"
    fi
  echo "----------------------------------------"
  fi
}

#----------------------------------------
#ここからスタート
#----------------------------------------

if [ "`whoami`" = "root" ];then
  :
else
  echo "Please sudo this !!"
  exit 1
fi

if [ $# -ne 0 ];then
  if [ $1 = "-v" ];then
    echo $PROGVER
    exit 0

  elif [ $1 = "-migrate" ];then
  #firefoxprof → firefoxprof.zipへ
#    sure
#    scvbackup scvrestore

    echo "Migration start `date '+%Y%m%d%H%M%S' `"
    echo "----------------------------------------"

    FIND=`find /home/ -name "firefoxprof" | grep -v "scvadmin"`
    echo "$FIND" | while read line
    do
      if [ ! -z "$line" ];then
        encryption "$line"
      fi
    done

    echo "----------------------------------------"
    echo "Migration complete `date '+%Y%m%d%H%M%S'`"

  elif [ $1 = "-restore" ];then
  #firefoxprof.zip → firefoxprofへ
#    sure
#    scvbackup scvrestore

    echo "Restore start `date '+%Y%m%d%H%M%S' `"
    echo "----------------------------------------"

    FIND=`find /home/ -name "firefoxprof.zip" | grep -v "scvadmin"`
    echo "$FIND" | while read line
    do
      if [ ! -z "$line" ];then
        composite "$line"
      fi
    done

    echo "----------------------------------------"
    echo "Restore complete `date '+%Y%m%d%H%M%S'`"

  else
    usage
  fi
else
  usage
fi


