transfer_original_games(){
    [ -d "$1" ] || return 1
    echo transfering original games...
    # Using temporary directory for original games
    tmp_games=/tmp/original_games
    mkdir -p "$tmp_games"    
    copy_mask "$mountpoint$gamepath/CLV-*" "$tmp_games"
    # Patching games to use our wrapper
    find $tmp_games -name *.desktop -exec sed -i -e 's/\/usr\/bin\/clover-kachikachi/\/bin\/clover-kachikachi-wr/g' {} +
    # We need to decide game distination directory, so we can't just copy all games at once
    for dir in $tmp_games/*; do
        if [ -d $dir ]; then
            code=$(basename "$dir")
            # File "hidden" contains list of unwanted original games, so we need to check it
            if [ -f $1/hidden ] && grep -q $code $1/hidden; then
                echo $code is hidden
                rm -rf $tmp_games/$code
            else
                # Now we trying to get destination directory
                if [ -f "$1/gpath-$code" ]; then
                    dist=`cat "$1/gpath-$code"`
                    mkdir -p "$tmp_games/$dist"
                    echo moving $code to $dist folder
                    mv "$tmp_games/$code" "$tmp_games/$dist"
                    rm "$1/gpath-$code"
                else                    
                    echo there is no path file for $code, moved to root
                fi
            fi
        fi
    done
    # Now we can write games to flash
    copy_mask "$tmp_games/*" "$target_dir"
    rm -rf "$tmp_games"
    rm -rf "$1"
}

transfer_games(){
    [ -d "$1" ] || return 1
    target_dir=$rootfs$gamepath
    # Flag to remove all installed games
    if [ -f "$1/clear" ]; then
        echo removing games and subfolders...
        rm -rf $target_dir/CLV-* $target_dir/???
        rm "$1/clear"
    fi
    mkdir -p "$target_dir"
    # But we need fonts
    restore "$gamepath/title.fnt"
    restore "$gamepath/copyright.fnt"
    transfer_original_games "$1/original"
    echo transfering new games...
    # this is simple
    [ "$gamepath" == "/usr/share/games" ] && find "$1" -name *.desktop -exec sed -i -e 's/\/games\/nes\/kachikachi/\/games/g' {} +
    copy_mask "$1/*" "$target_dir"
    # need to reset menu state
    rm -f $installpath/menu
}
