#!/bin/bash

##
#  Korinf project
#
#  Run command on all systems
#  Usage: korexec DISTR_LIST command line
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2009, 2011, 2013
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009, 2011, 2013
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  This program 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 Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/korinf/common

kormod mount list

if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
	echo "korexec - executes command in chroot system"
	echo "Use: korexec System/Version <command line>"
	exit 0
fi

REBUILDLIST=$1
set_rebuildlist
shift

[ -n "$1" ] || fatal "Command line is empty"

for dist in $(get_distro_list $REBUILDLIST) ; do
    dist=$(get_real_target_dist $dist)

    # fills $BUILDARCH, $DISTRNAME, $DISTRVERSION
    parse_dist_name $dist

    if [ "$DISTRNAME" = "ALTLinux" ] ; then
        echo "There is no chroot for ALT Linux (hasher used), skipped $dist"
        continue
    fi

    echo
    echo "############"
    mount_linux $dist || continue

    run_in_chroot "$*"

    end_umount

#    RES=$?
done

#exit $RES
