#!/bin/bash -ex
# Needed settings in /etc/sudoers.
#disklessfun	ALL=(ALL) NOPASSWD: /usr/bin/volname
#disklessfun	ALL=(ALL) NOPASSWD: /bin/mount
#disklessfun	ALL=(ALL) NOPASSWD: /bin/umount

gen-autorun-inf()
{
local dirpath=${1}
echo -e "[autorun]\r">"${dirpath}/autorun.inf"
echo -e 'open=explorer.exe /n,\\'"\r">>"${dirpath}/autorun.inf"
}

escapepattn()
{
local pattn=${1}
pattn="`echo "${pattn}"|sed -e 's/\[/\\\[/'g`"
pattn="`echo "${pattn}"|sed -e 's/\]/\\\]/'g`"
escapedmatchpattn=${pattn}
}
escapedmatchpattn=

test $# -lt 1 && echo "eg. $0 hoge.iso[img,dmp,tgz, etc]" && exit 1
SRCFILE=$1
filename=${SRCFILE##*/}
ext=${filename##*\.}
test "${ext}" = "${filename}" && echo "Extension not found" && exit 1
basename=${filename%*\.$ext}
dirname=${SRCFILE%*/*}
test "${dirname}" = "${filename}" && dirname=.
MNTDIR=$(mktemp -d)
set +e
sudo mount -o ro,loop "${SRCFILE}" ${MNTDIR}
RET=$?
set -e
if [ $RET -eq 0 ]; then
escapepattn "${SRCFILE}"
LODEV=$(losetup -a|grep "${escapedmatchpattn}"|cut -d":" -f1|tail -1)
#VOLNAME="$(sudo volname ${LODEV}|tr -cd '0-9A-Z_')"
VOLNAME="$(sudo volname ${LODEV}|tr -d '\040')"
sudo umount ${MNTDIR}
else
VOLNAME=SPLITTED
fi
rmdir ${MNTDIR}
test -z "${VOLNAME}" && VOLNAME=DVD_VIDEO
vollength=${#VOLNAME}
#test ${vollength} -gt 30 && VOLNAME=${VOLNAME:0:30}
test ${vollength} -gt 27 && VOLNAME=${VOLNAME:0:27}
filesize=$(stat -c %s "${SRCFILE}")
filesizeM=$(($filesize / 1048576)) 
halfsizeM=$(($filesizeM / 2))

exitproc1()
{
rm -rf "${VOLNAME}_1" "${VOLNAME}_2"
}

trap '
exitproc1
exit 1
' 2 15

mkdir "${VOLNAME}_1" "${VOLNAME}_2"
gen-autorun-inf "${VOLNAME}_1"
gen-autorun-inf "${VOLNAME}_2"
set +x
basenames="$(echo "${basename}"|iconv -f UTF-8 -t CP932)"
echo -n "COPY /B \"${basenames}.000\"+\"${basenames}.001\" \"${basenames}.$ext\""> \
"${VOLNAME}_2/${basename}.bat"
set -x
echo -e "\r">>"${VOLNAME}_2/${basename}.bat"
WD=$(pwd)
/bin/bash -ex -c "cd "${dirname}";md5sum \"${filename}\" > \
"${WD}/${VOLNAME}_2/md5sum.txt""

dd if="${SRCFILE}" bs=1M count=$halfsizeM of="${VOLNAME}_1/${basename}.000"
#mkisofs -V "${VOLNAME}_1" -udf -r -o "${VOLNAME}_1.iso" "${VOLNAME}_1"
genisoimage -V "${VOLNAME}_1" -allow-limited-size -r -o "${VOLNAME}_1.iso" "${VOLNAME}_1"
rm -rf "${VOLNAME}_1"

dd if="${SRCFILE}" bs=1M skip=$halfsizeM of="${VOLNAME}_2/${basename}.001"
#mkisofs -V "${VOLNAME}_2" -udf -r -o "${VOLNAME}_2.iso" "${VOLNAME}_2"
genisoimage -V "${VOLNAME}_2" -allow-limited-size -r -o "${VOLNAME}_2.iso" "${VOLNAME}_2"
orghash="$(cat "${VOLNAME}_2/md5sum.txt")"
rm -rf "${VOLNAME}_2"

exitproc2()
{
sudo umount "${MNTDIR1}"
sudo umount "${MNTDIR2}"
rmdir ${MNTDIR1} ${MNTDIR2}
}

trap '
exitproc2
exit 1
' 2 15
MNTDIR1=$(mktemp -d)
MNTDIR2=$(mktemp -d)
sudo mount -o loop,ro "${VOLNAME}_1.iso" "${MNTDIR1}"
sudo mount -o loop,ro "${VOLNAME}_2.iso" "${MNTDIR2}"
newhash="$(cat "${MNTDIR1}/${basename}.000" "${MNTDIR2}/${basename}.001"|md5sum)"
set +x
echo;
echo "Original hash value."
echo "${orghash}";echo
echo "Archived hash value."
echo "${newhash}";echo
set -x
exitproc2
set +x
echo "Done."
