#!/bin/bash

if [ $# -ge 1 ]; then
	PACK_NAME=$1
else
        echo Usage $0 GameName /mount/point
        exit 1
fi
. ./pack.config #this script requires PACK_NAME to be defined!

if [ $# -ge 2 ]; then
        MOUNTPOINT=$2
else
        echo Usage $0 GameName /mount/point
	echo Mount poit was not provided, using default
	echo
	MOUNTPOINT=./$PACK_NAME
fi
mkdir -p $MOUNTPOINT

if [ $ENCRYPT_PACK -gt 0 ]; then
	IMAGE_FILE=$DEST_PATH/$PACK_NAME.cpack
else
	IMAGE_FILE=$DEST_PATH/$PACK_NAME.pack
fi
	if [ ! -f $IMAGE_FILE ]; then
		echo "Game file $IMAGE_FILE was not found"
		exit 1
	fi

#ni enak za ruleto	cp -f ../../run.sh $GAME_NAME/run.sh

	echo "Mounting $PACK_NAME on $MOUNTPOINT, please wait..."

	#setup loopback device
#	losetup -fP $IMAGE_FILE
#	LOOPBACK_DEV=`losetup -a|grep $IMAGE_FILE|cut -d':' -f1-1`
#	echo Got LoopbackDevice $LOOPBACK_DEV
	#open encryptedpack

#ni potrebno rocno narediti loopback device, ga ze cryptsetup sam ustvari, in se nato tudi sam brise
#	cryptsetup open $LOOPBACK_DEV encryptedpack --type plain -c twofish << END_OF_SESSION
if [ $ENCRYPT_PACK -gt 0 ]; then
	cryptsetup open $IMAGE_FILE encryptedpack --type plain --hash sha256 --key-size=256 <<< najdinasna15maj2b
	ENCRYPTED_PACK=/dev/mapper/encryptedpack
else
	ENCRYPTED_PACK=$IMAGE_FILE
fi

	mount -o loop $ENCRYPTED_PACK $MOUNTPOINT

	if [ $? -eq 0 ]; then 
		echo "Pack mounted on $MOUNTPOINT"
	else
		echo "Error mounting pack on $MOUNTPOINT"
	fi
	echo

