#!/bin/sh
#PFS module creator 
#VERSION 3.7
. $(which pfs)

D(){
    sourcelist="$(find "$1" -maxdepth 1 -mindepth 1 -type d |sort)"
    [ "$sourcelist" ] || exitmsg "no directory in '$2'" 1
    ondir=
    [ "${userout}_" = "_" ] && [ ! "`echo "$arglist" |grep "\-o "`" ] && userout="./$(basename "$1").$EXT"
}
M(){
    sourcelist="$(find "$1" -maxdepth 1 -mindepth 1 -type f |egrep ".pfs$|.xzm$|.sfs$|.squashfs$|.$EXT$" |sort)"
    [ "$sourcelist" ] || exitmsg "no modules in '$2'" 1
    onmod=
    [ "${userout}_" = "_" ] && [ ! "`echo "$arglist" |grep "\-o "`" ] && userout="./$(basename "$1").$EXT"
}


#help
HLP(){
echo "Usage: $0 <list of sources> -o out.$EXT"
echo "Examples:"
echo "$0 ./dir 			-make dir.pfs from dir"
echo "$0 1.$EXT 2.$EXT -o 3.$EXT	-collect 1.$EXT and 2.$EXT to contaner 3.$EXT"
echo "$0 * -o ./big.$EXT		-collect all dirs and squashfs modules from current dir to big.$EXT" 
echo
echo "Параметры (ключи):"
echo "	-o / --out-file - указывает название выходного файла (если указать без расширения .$EXT на выходе будет каталог)."
echo "	-w - не включать AUFS тени."
echo "	-l / --local - сборка на месте, без использования aufs."
echo "	-f / --fast - быстрая компрессия (размер .$EXT больше, но создается быстрее)."
echo "  --mkilst  add files list to simple (non contaner) module"
echo "	--mksqfs / параметры для mksquashfs, компрессия размер блока и проч.(Внимание параметр должен быть последним)"
echo "Алиасы (сокращения):"
echo " -d | -D - Только каталоги"
echo "	$(basename $0) -d dir ---> $(basename $0)" '$(find dir -maxdepth 1 -mindepth 1 -type d ) -o dir.$EXT'
echo " -m | -M - Только модули"
echo "	$(basename $0) -m dir ---> $(basename $0)" '$(find dir -maxdepth 1 -mindepth 1 -type f ) -o dir.$EXT'
exit 1
}

check_kernel(){
checksfsxzb >/dev/null 2>&1
exitmsg "Kernel is not support squashfs/aufs. Work only 'mkpfs -l'" $?
}

#parsing for "--mksqfs" arg
argslist="$@"
if  echo $argslist |grep -q "\-*mksqfs .*" ; then
	compression="$(echo $@ |sed 's/^.*\-*mksqfs//')"
	argslist="$(echo $@ |sed 's/\-*mksqfs.*$//')"
fi

#get opts
sourcelist=""
inplace=""
for arg in $argslist
do
  case "${arg}" in
    "-o" | "--out-file") onuserout="on";;
    "-d" | "-D") ondir="on";;
    "-m" | "-M") onmod="on";;
    "-h" | "--help")  HLP ;exit 1;;
    "-q" | "--quiet" ) devnull='>/dev/null' ;;
    "-no-progress" | "--no-progress") noprogress="-no-progress";;
    "-processors" | "--processors" ) numproc="on";;
    "-w") wh="-regex -e ".wh..wh."";;
    "-l" | "--local" ) inplace=yes ;;
    "--mklist" ) make_list=yes ;;
    "-f" | "--fast" | "-g" ) [ "$compression_fast" ] && compression="$compression_fast" || compression="gzip";;
    "-"*[A-Za-z]*) echo "$(basename "$0"): invalid option -- '$(echo ${arg} | tr -d '-')'" >&2; HLP; exit 1;;
    *) if [ "${usepkname}" = "on" ]; then packname="${arg}"
       elif [ "${ondir}" = "on" ]; then D "${arg}"
       elif [ "${onmod}" = "on" ]; then M "${arg}"
       elif [ "${onuserout}" = "on" ]; then userout="${arg}"
       elif [ "${numproc}" = "on" ]; then useproc="-processors ${arg}"
       else sourcelist="${sourcelist} ${arg}"; fi
       onuserout="off"; usepkname="off"; usindlib="off"; numproc="off";;
  esac
done

[ "$sourcelist" ] || HLP 

allow_only_root

[ "$inplace" ] || check_kernel

if [ "${userout}_" = "_" ] ; then 
  first="$(basename $(echo $sourcelist | awk '{print $1}'))" 
  userout="$(echo ${first%.$EXT}).$EXT" 
fi


#1 dir, in place mode
if [ -d $sourcelist 2>/dev/null -a "$inplace" == "yes" ] ;then
    eval echo "==== In place mode ====" $devnull
    [ ! "$(ls -a $sourcelist)" ] && exitmsg "directory '$sourcelist' empty" 1
    [ $make_list ] && mklist $sourcelist $sourcelist ${userout%.$EXT} 
    mksqmod $sourcelist "${userout}" && exit 0 
    exitmsg "mkpfs error" 2
fi

#test sources, source must be dir or squashfs module
for source in $sourcelist ;do
	[ -d $source ] && continue
	[ "$(fs_type $source)" == "squashfs" ] && continue
	exitmsg "Type of source \"$source\" is not correct" 2
done	

[ -d "$sourcelist" ] && { ls "$sourcelist" 2>/dev/null || exitmsg "\"`echo $sourcelist`\" is empty" 2 ; }

#make root aufs
n="$(mkaufs || exitmsg "mkaufs error" 2)"
nn="$(echo "$n" | sed -n 's/^.*\([0-9]\)$/\1/p')"
[ -d "$n" ] || exitmsg "error mounting aufs" 3

#add sources as aufs layers
for  i in $sourcelist ;do
    eval addlayer "$nn" "$i" "$devnull" || exitmsg "can't insert layer to aufs $nn" 5
done 

if [ $(echo "$sourcelist" | wc -w) -eq 1 ] ;then
  # if source is only one - submodule name is $userout with no prefix (pfs,xzm,etc)
  [ $make_list ] && mklist "$i" "$n" $([ $(echo $sourcelist  |wc -w) -eq 1 ] && echo ${userout%.$EXT} )
else	
  bundles=$(find $(echo $n |sed 's:aufs:bundles:') -maxdepth 1 -mindepth 1 -type d )
  #mklist for modules, submodule name is basename source with no prefix
  for i in $bundles ;do
	name="$(basename "${i%.$EXT}")" 
	mklist "$i" "$n" "$name"
  done 
fi

if echo ${userout} | grep -q "^.*\.$EXT$" ; then 
	mksqmod "${n}" "${userout}" noexit
else
	echo "Please wait..."
	rmdir ${userout} 2>/dev/null
	[ -d "${userout}" ] && exitmsg "Output diectory already exist" 6
	mkdir -p "$(dirname ${userout})"
	cp -fr "${n}" "${userout}"
fi

exitmsg "mksqmod error" $?  noexit
delaufs "$nn" 
exitmsg "delaufs error" $?
