#!/bin/bash

set -xe
set -o pipefail

if [ -z "$1" ]; then
  echo "Usage: $0 <src-folder-path>"
  exit 1
fi

tmpFolder=$(mktemp -d)

deleteTmpFolder() {
  rm -rf $tmpFolder
}

trap deleteTmpFolder EXIT

wd=$(pwd)
encName="lbk-$(date +%Y%m%d)-enc.zip"

# 7z a -tzip -p -mem=AES256 archive.zip file1 file2 directory/

# compress Source.zip without encryption
cd $1

zip -r "$tmpFolder/Source.zip" .

# generate Source.zip md5
md5sum "$tmpFolder/Source.zip" | cut -d' ' -f1 | tee "$tmpFolder/Vendor_Src_MD5.txt"

# compress with encryption
cd $tmpFolder

7z a -tzip -pLb1234567890 -mem=AES256 "$tmpFolder/$encName" Source.zip Vendor_Src_MD5.txt

# copy to pwd folder
cp "$tmpFolder/$encName" $wd
cd $wd
