#!/bin/bash

########################################################################
#                                                                      #
# mount-iso                                                            #
#                                                                      #
# Copyright (C) 2020 PJ Singh <psingh.cubic@gmail.com>                 #
#                                                                      #
########################################################################

########################################################################
#                                                                      #
# This file is part of Cubic - Custom Ubuntu ISO Creator.              #
#                                                                      #
# Cubic is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# Cubic is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with Cubic. If not, see <http://www.gnu.org/licenses/>.        #
#                                                                      #
########################################################################

# Mount an ISO for Cubic.

########################################################################
# Arguments
########################################################################

program=${0}
number_arguments=${#}
iso_image_file_path=${1}
iso_image_mount_point=${2}
user_id=${3}
group_id=${4}

# echo "program..................... ${program}"
# echo "number of arguments......... ${number_arguments}"
# echo "iso image file path......... ${iso_image_file_path}"
# echo "iso image mount point....... ${iso_image_mount_point}"
# echo "user id..................... ${user_id}"
# echo "group id.................... ${group_id}"

########################################################################
# Command
########################################################################

# Error codes
#  1 = incorrect invocation permission
#  2 = system error (out of memory, cannot fork, no more loop devices)
#  4 = internal mount bug or missing nfs support in mount
#  8 = user interrupt
# 16 = problems writing or locking /etc/mtab
# 32 = mount failure
# 64 = some mount succeeded

if [[ ${user_id} && ${group_id} ]]; then
    mount --options loop,uid="${user_id}",gid="${group_id}" "${iso_image_file_path}" "${iso_image_mount_point}"
elif [[ ${user_id} ]]; then
    mount --options loop,uid="${user_id}" "${iso_image_file_path}" "${iso_image_mount_point}"
elif [[ ${group_id} ]]; then
    mount --options loop,gid="${group_id}" "${iso_image_file_path}" "${iso_image_mount_point}"
else
    mount --options loop "${iso_image_file_path}" "${iso_image_mount_point}"
fi

