#!/bin/bash

DEVICE="$1"
MOUNTPOINT="/tmp/usbcopy"
SOURCE_DIR="/home/robot/RobotControl/RobotControlLogs"
SOURCE_DIR2="/home/robot/RobotControl"


# Zielordner und ZIP-Datei
DEST_DIR="$SOURCE_DIR2/RobotControlLogs"
ZIP_FILE="$SOURCE_DIR2/RobotControlLogs.zip"


# Ordner neu erstellen
if [ -d "$DEST_DIR" ]; then
    rm -rf "$DEST_DIR"
fi

mkdir -p "$DEST_DIR"

# Dateien kopieren
cp "$SOURCE_DIR2/log01.txt" "$DEST_DIR/"
cp "$SOURCE_DIR2/log02.txt" "$DEST_DIR/"
cp "$SOURCE_DIR2/log03.txt" "$DEST_DIR/"
cp "$SOURCE_DIR2/log04.txt" "$DEST_DIR/"
cp "$SOURCE_DIR2/log05.txt" "$DEST_DIR/"


if [ -z "$DEVICE" ]; then
 
  exit 1
fi



# Warten auf Gerät
for i in {1..10}; do
    if [ -b "$DEVICE" ]; then
      
        break
    fi
    sleep 1
done

mkdir -p "$MOUNTPOINT" 
if [ ! -d "$MOUNTPOINT" ]; then

  exit 1
fi

mount "$DEVICE" "$MOUNTPOINT" 
MOUNT_OK=$?

if [ $MOUNT_OK -ne 0 ]; then
    exit 1
fi




 # Ordner prüfen und kopieren
    if [ -d "$SOURCE_DIR" ]; then
        cp -r "$SOURCE_DIR" "$MOUNTPOINT/" 
    fi


cd $MOUNTPOINT	
	# Ordner neu erstellen
if [ -f RobotControlLogs.zip ]; then
    rm -f RobotControlLogs.zip
fi
	
# Zip Datei auf USB Stick erstellen

zip -r RobotControlLogs.zip RobotControlLogs


# loeschen der RobotControlLogs Ordner auf dem USB Stick und dem pi
sudo rm -r "$SOURCE_DIR"
sudo rm -r "$MOUNTPOINT/RobotControlLogs"


    # Sicherstellen, dass alles geschrieben wurde
    sync
	

